import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
使用FileReader FileWriter进行拷贝的话,只能拷贝“普通文本”文件
*/
public class Copy02 {
public static void main(String[] args) {
FileReader in = null;
FileWriter out = null;
try {
// 读
in = new FileReader("E:\\JAVAhw\\IO\\src\\Copy01.java");//java.io.FileNotFoundException: maildata.txt (系统找不到指定的文件。)
// 写
out = new FileWriter("Copy01.java");
// 一边读一边写
char[] chars = new char[1024 * 512]; // 1MB
int readCount = 0;
while ((readCount = in.read(chars)) != -1){
out.write(chars,0,readCount);
}
// 不要忘记刷新
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
关于FileReader 和FileWriter 的拷贝问题
最新推荐文章于 2024-09-15 09:47:04 发布