package com.io;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import org.junit.Test;public class 缓冲字符流 { @Test public void bufferedReaderTest(){ File file = new File("hello.txt"); FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(file); br = new BufferedReader(fr); /** * 使用旧的方法,读到一个char数组,不写了 * 下面使用readline方法,最后一行返回null */ String str; while((str = br.readLine()) != null){ System.out.println(str); } } catch (IOException e) { e.printStackTrace(); }finally{ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test public void bufferedWriterTest(){ File file = new File("hello.txt"); File fileOut = new File("bf_reader.txt"); FileReader fr = null; FileWriter fw = null; BufferedReader br = null; BufferedWriter bw = null; try { fr = new FileReader(file); br = new BufferedReader(fr); fw = new FileWriter(fileOut); bw = new BufferedWriter(fw); /** * 使用旧的方法,读到一个char数组,不写了 * 下面使用readline方法,最后一行返回null */ String str; while((str = br.readLine()) != null){ bw.write(str); bw.newLine();//换行 bw.flush();//flush } } catch (IOException e) { e.printStackTrace(); }finally{ //先关输出流 try { bw.close(); } catch (IOException e) { e.printStackTrace(); } try { br.close(); } catch (IOException e) { e.printStackTrace(); } } }}