java io流之BufferReader&BufferedWriter

时间:2022-01-27 05:16:28

BufferedReader 由Reader类扩展而来,提供通用的缓冲方式文本读取,而且提供了很实用的readLine,读取一个文本行,从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。

一般用法:

@Test
public void testBufferReader() throws IOException {
InputStream in = new FileInputStream(charStreamPath + File.separator + "1.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line; while((line = br.readLine())!=null) {
System.out.println(line);
}
br.close(); BufferedWriter bw = new BufferedWriter(new FileWriter(charStreamPath +
                                     File.separator + "1.txt", true));
bw.write("BufferedWriter追加的");
bw.flush();
bw.close();
}