import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 字符流实现文件复制(异常捕获)
*/
public class FileTest1 {
public static void main(String[] args) {
//调用方法 bb.txt 给定15M 左右
CopyFile("D:\\filetest\\bb.txt","D:\\filetest\\a\\bb.txt");
}
//复制方法
public static void CopyFile(String fr1 ,String fw1){
FileReader fr = null;
FileWriter fw = null;
try {
//声明一个读的文件字符流
fr = new FileReader(fr1);
//声明一个写的文件字符流
fw = new FileWriter(fw1);
//自建缓冲区
//定义一个字符数组
char[] cs = new char[1024*1024];
//如果读取到的值为-1,则说明已经读取完
int len =-1;
while ((len = fr.read(cs))!=-1){
//边读边写,把读取的内容放入数组中,再把数组中的内容往外写
fw.write(cs,0,len);
//手动冲刷
fw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//判断文件字符输出流对象是否初始化成功
if (fw!=null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
//流对象置为null
fw = null;
}
}
if (fr!=null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
//流对象置为null
fr = null;
}
}
}
}
}