package IO;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
// 用字符流只能拷贝普通文本文件
public class Copy02 {
public static void main(String[] args) {
FileReader in = null;
FileWriter out = null;
try {
// 创建输入输出对象
in = new FileReader("src/IO/Copy02.java");
out = new FileWriter("Copy02.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();
}
}
}
}
}
javaSE用字符IO流拷贝文件
最新推荐文章于 2024-05-16 10:23:10 发布