1、使用缓冲字节流copy大型文件;
// 使用try-resource-with,系统自动关闭资源,资源需要实现Closeable接口或者继承AutoCloseable类
try (BufferedInputStream is = new BufferedInputStream(new FileInputStream("1111.txt"));
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream("1112.txt"));) {
int len = 0;
while ((len = is.read()) != -1) {
os.write(len);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2、使用缓冲字符流copy大型文件;
// 使用try-resource-with,系统自动关闭资源,资源需要实现Closeable接口或者继承AutoCloseable类
try (BufferedReader br = new BufferedReader(new FileReader("1111.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("11112.txt"))) {
char[] datas = new char[10];
int len = 0;
while ((len = br.read(datas)) != -1) {
bw.write(datas, 0, datas.length);
}
bw.flush();
} catch (Exception e) {
e.printStackTrace();
}
总结
缓冲流的默认大小为8k,即在写入的时候内容没有超过8k,不使用flush方法的情况下是不会写入文件的