package day20150904;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
/**
* 字符流只用於讀寫文本數據
* 字符流是高級流,底層本質上還是讀寫字節
*
* Reader,Writer:字符輸入輸出流的父類
*/
public class OutputStreamWriterDemo1 {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("t.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
osw.write("hello");
osw.write("中華人民共和國");
osw.write("成立了");
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
package day20150904;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class InputStreamReaderDemo2 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("t.txt");
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
int d = -1;
while((d=isr.read())!=-1){
char c = (char)d;
System.out.print(c);
}
isr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package day20150904;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class FileCopy3 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("."
+File.separator+"src"
+File.separator+"day0831"
+File.separator+"FileCopy3.java");
FileOutputStream fos = new FileOutputStream("copy.java");
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
int d = -1;
while((d=isr.read())!=-1){
osw.write(d);
}
isr.close();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
package day20150904;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
/**
* PrintWriter:
* 緩衝字符輸出流,帶有自動行刷新
* 可以以行爲單位寫出字符串
*
* BufferedWriter沒有PrintWriter用起來爽
*/
public class PrintWriterDemo4 {
public static void main(String[] args) {
try {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("pw.txt"),"utf-8");
/**
* true:自動行刷新:
* 每當使用println方法(不是print)後,都會自動flush
*
*注意:會增加寫的次數,降低寫出效率
*/
PrintWriter pw = new PrintWriter(osw,true);
pw.println("你好:");
pw.print("hehe");
pw.println("今天");
pw.println("天氣不錯");
osw.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
}
package day20150904;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
/**
* BufferedReader:緩衝字符輸入流
* 可以以行爲單位讀取字符串
*/
public class BufferedReader5 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("pw.txt");
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
BufferedReader br = new BufferedReader(isr);
/**
* String readLine()
* 讀取一行,讀取到換行符爲止
* 如果返回值爲null,說明沒數據可讀了
*/
String str = null;
while((str=br.readLine())!=null){
System.out.println(str);
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package day20150904;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
public class FileReadWriteDemo6 {
/**
* 文件中內容如下
* hello
* 你好
* 今天天氣不錯
* 要求操作後文件內容如下
* hello
* 你好
* 今天天氣不錯
* hello你好今天天氣不錯
*/
public static void main(String[] args){
try {
FileInputStream fis = new FileInputStream("pw.txt");
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String str = null;
while((str=br.readLine())!=null){
sb.append(str);
}
br.close();
FileOutputStream fos = new FileOutputStream("pw.txt",true);
OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
PrintWriter pw = new PrintWriter(osw);
pw.println(sb);
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}