呵呵,哥们,我也刚学到缓冲区,就我的见解来说一下吧,API中是这样写到的将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。BufferedWriter字符流缓冲区,也可以说是一个装饰类,他提供了Writer类中写入方法中所没有的,newLine方法,这个方法可以写入一个换行符- /*IO_通过缓冲区复制文本文件*/
- import java.io.*;
- class CopyBufDemo
- {
- public static void main(String[] args)
- {
- BufferedReader bufr = null;
- BufferedWriter bufw = null;
- String len = null;
- try
- {
- bufr = new BufferedReader(new FileReader("CopyTest.java"));
- bufw = new BufferedWriter(new FileWriter("CopyTest_Copy.txt"));
- while ((len = bufr.readLine()) != null) //一次读取一行的内容
- {
- bufw.write(len); //写入
- bufw.newLine(); //换行
- bufw.flush(); //刷新缓冲区
- }
- }
- catch (IOException e)
- {
- System.out.println(e.toString());
- }
- finally
- {
- try
- {
- if(bufr != null)
- bufr.close();
- }
- catch (IOException e)
- {
- System.out.println(e.toString());
- }
- try
- {
- if(bufw != null)
- bufw.close();
- }
- catch (IOException e)
- {
- System.out.println(e.toString());
- }
- }
- }
- }
复制代码 |