欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 缓冲区复制文本文件

程序员文章站 2022-03-25 22:45:56
public class CopyTextByBuf { public static void main(String[] args) { BufferedReader bufr = null; BufferedWriter bufw = null; try { bufr = new Buffere ......
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()一次读一个的方法。