[Java] 字符流 Writer,输出字符数据PrintWriter

时间:2023-03-08 20:30:38
package test.stream;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter; /**
* 字符流 Writer
* @author Frost.Yen
* @E-mail 871979853@qq.com
* @date 2016年4月13日
*/
public class TestWriter {
public static void main(String[] args) {
BufferedReader br = null;
PrintWriter out = null;
try {
//字符流用来读取字符数据,对于输入字符流而言,最为常用的操作方法是使用BufferedReader,因为该流有个readLine()
br = new BufferedReader(new FileReader("E:\\JAVA\\Examples\\To Learn\\src\\test\\stream\\test.txt"));
out = new PrintWriter(new BufferedWriter(new FileWriter("E:\\JAVA\\Examples\\To Learn\\src\\test\\stream\\tt.txt")));
//BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\JAVA\\Examples\\To Learn\\src\\test\\stream\\tt.txt"));
String str = null;
while((str = br.readLine())!=null){
System.out.println(str);
//使用bw输出不会换行,得调用newLine()换行
//bw.write(str+"\n");
//bw.newLine(); out.println(str);
}
//bw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(br!=null) br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(out!=null) out.close();
} }
}