package test_demo.fileoper;
import java.io.*;
/*
* 字符输入输出流操作,复制文件
* 使用缓冲流扩展,逐行复制
* */
public class FileReadWriteOper {
public static void main(String args[]) {
//字符流输入流
FileReader fr = null;
//字符流输出流
FileWriter fw = null;
//缓冲输入流
BufferedReader br = null;
//缓冲输出流
BufferedWriter bw = null;
try {
fr = new FileReader(new File("C:\\testdata\\filedir\\a.txt"));
br = new BufferedReader(fr); //扩容,类似加水管
fw = new FileWriter("C:\\testdata\\filedir\\c.txt");
bw = new BufferedWriter(fw);
//逐行复制
String line = br.readLine();
while (line != null) {
bw.write(line);
bw.newLine(); //换行输出
line = br.readLine();
}
System.out.println("文件复制成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//关闭流,顺序与打开相反
bw.close();
br.close();
fw.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}