代码如下:
package moose;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class FileCopyTool2 {
//测试
public static void main(String[] args) {
Copyfile2("d:/a.txt","d:d.txt");
}
//基于字符缓冲流实现文件拷贝
public static void Copyfile2(String src,String des){
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(src));
bw = new BufferedWriter(new FileWriter(des));
String temp = "";
while ((temp=br.readLine())!=null){
bw.write(temp);
bw.newLine(); //每次读一行所有要加换行符
}
bw.flush();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(br != null){
br.close();
}
if (bw != null){
bw.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}