使用缓冲区修改复制文本文件

以缓冲区的方式修改第六条中复制文本文件的代码


import java.io.*;

public class  CopyTextByBuf
{
    public static void main(String[] args) 
    {
        BufferedReader bufr = null;
        BufferedWriter bufw = null;

        try
        {
            bufr = new BufferedReader(new FileReader("demo.txt"));
            bufw = new BufferedWriter(new FileWriter("demoCopy.txt"));

            String line = null;

            while((line=bufr.readLine())!=null)
            {
                bufw.write(line);
                bufw.newLine();//readLine()并不读取换行符,要自行换行
                bufw.flush();
            }
        }
        catch (IOException e)
        {
            throw new RuntimeException("读写失败");
        }
        finally
        {
            try
            {
                if(bufr!=null)
                    bufr.close();
            }
            catch (IOException e)
            {
                throw new RuntimeException("读取关闭失败");
            }
            try
            {
                if(bufw!=null)
                    bufw.close();
            }
            catch (IOException e)
            {
                throw new RuntimeException("写入关闭失败");
            }
        }
    }
}

你可能感兴趣的:(使用缓冲区修改复制文本文件)