欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java 缓冲区复制文本文件,

来源: javaer 分享于  点击 2244 次 点评:219

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()一次读一个的方法。


相关文章

    暂无相关文章
相关栏目:

用户点评