329.-io流(字符-练习-复制文本文件二)

时间:2022-04-01 21:00:43

//每次读取的字节长度,一般都是1024的倍数
private static final int BUF_SIZE = 1024;

public static void main(String[] args) {
// TODO Auto-generated method stub
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader("demo.txt");
fw = new FileWriter("cope.txt");
//定义一个数组容器,用来装读取的字节
char[] buf = new char[BUF_SIZE];
int len = 0;
while ((len = fr.read(buf)) != -1) {
fw.write(buf, 0, len);
}

} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException("复制失败");
}finally {
if (fr != null) {
try {
fr.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
if (fw != null) {
try {
fw.close();
} catch (Exception e2) {
// TODO: handle exception
}
}

}



}