码迷,mamicode.com
首页 > 其他好文 > 详细

IO知识点汇总3

时间:2017-09-21 19:24:34      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:buffered   div   字节   red   相关   com   new   col   ade   

10、Reader和Writer

  Reader和Writer是所有字符流的抽象基类,使用上和字节流类似,把byte改成char就可以。掌握了字节流,字符流很容易就可以掌握。

    public static void test1() {
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new FileReader(path + "result.txt"));
            writer = new BufferedWriter(new FileWriter(path + "result1.txt"));
            String line;
            while ((line = reader.readLine()) != null) {
                writer.write(line, 0, line.length());
                writer.newLine();
            }
        } catch (Exception e) {
        } finally {
            org.apache.commons.io.IOUtils.closeQuietly(reader);
            IOUtils.closeQuietly(writer);
        }
    }

    public static void test2() {
        BufferedWriter writer = null;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(path + "result.txt"));
            writer = new BufferedWriter(new FileWriter(path + "result1.txt"));
            char buff[] = new char[1024];
            int len;
            while ((len = reader.read(buff)) != -1) {
                writer.write(buff, 0, len);
            }
        } catch (Exception e) {

        } finally {
            IOUtils.closeQuietly(writer);
            IOUtils.closeQuietly(reader);
        }
    }

java IO先整理到这里,后面有碰到相关的问题会继续做整理

IO知识点汇总3

标签:buffered   div   字节   red   相关   com   new   col   ade   

原文地址:http://www.cnblogs.com/Xhua/p/7569943.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!