BufferedReader&BufferedWriter
package com.gao.xi;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import com.sun.java_cup.internal.runtime.virtual_parse_stack;
public class TestAllEng3 {
public static void main(String[] args) {
/*
* 1.获取当前类的所在路径
* 2.获取两个文件的路径
* 3.声明FileReader和FileWriter变量
* 4.实例化文件输出输入流
* 5.读写文件使用BufferedReader/Writer
* 6.关闭流
*/
// * 1.获取当前类的所在路径
String path = "src/com/gao/xi/";
// * 2.获取两个文件的路径
String inpath = path + "inputstream.txt";
String outpath = path + "outputstream.txt";
// * 3.声明FileInputStream和FileOutStream变量
FileReader fin = null;
FileWriter fout = null;
BufferedReader br=null;
BufferedWriter bw=null;
try {
// * 4.实例化文件输出输入流
fin = new FileReader(inpath);
fout = new FileWriter(outpath,true);
// * 5.读写文件
br=new BufferedReader(fin);
bw=new BufferedWriter(fout);
String temp="";
StringBuilder stringBuilder=new StringBuilder();
while ((temp = br.readLine()) != null) {
bw.write(temp);
bw.newLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// * 6.关闭流
if(bw!=null)
{
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
if(br!=null){
br.close();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}