BufferedReader
BufferedWriter
package com.winson.iotest;
import org.junit.Test;
import java.io.*;
/**
* @description:输入、输出字符缓冲流整合(实现复制文本文件)
* @date: 2020/7/5 21:32
* @author: winson
*/
public class BufferedReadBufferedWriterTest {
@Test
public void test1() {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
File srcFile = new File("hello.txt");
File desFile = new File("hello_out.txt");
FileReader fileReader = new FileReader(srcFile);
FileWriter fileWriter = new FileWriter(desFile);
bufferedReader = new BufferedReader(fileReader);
bufferedWriter = new BufferedWriter(fileWriter);
// 方式一
// char[] chars = new char[1024];
// int len;
// while ((len = bufferedReader.read(chars)) != -1) {
// bufferedWriter.write(chars, 0, len);
// }
// 方式二
String data;
while ((data = bufferedReader.readLine()) != null) {
// 写入方式一
// bufferedWriter.write(data + "\n");
// 写入方式二
bufferedWriter.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}