java 缓冲区复制文本文件

public class CopyTextByBuf {
    public static void main(String[] args) {
        BufferedReader bufr = null;
        BufferedWriter bufw = null;
        try {
            bufr = new BufferedReader(new FileReader("demo_src.txt"));
            bufw = new BufferedWriter(new FileWriter("demo_desc.txt"));
            String line = null;
            //readLine不带行终止符
            while ((line = bufr.readLine()) != null) {
                bufw.write(line);
                bufw.newLine();
                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("写入关闭失败!");
            }
        }
    }
}
newLine()
无论读一行还是获取多个字符,其实最终都是在硬盘上一个一个读取。所以最终使用的还是read()一次读一个的方法。


猜你喜欢

转载自www.cnblogs.com/hongxiao2020/p/12677107.html
今日推荐