当前位置: 移动技术网 > IT编程>开发语言>Java > java 缓冲区复制文本文件

java 缓冲区复制文本文件

2020年04月11日  | 移动技术网IT编程  | 我要评论
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()一次读一个的方法。


如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网