IO知识点汇总3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IO知识点汇总3相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

代码小知识点汇总3

java内存流:java.io.ByteArrayInputStreamjava.io.ByteArrayOutputStreamjava.io.CharArrayReaderjava.io(代码片段

java缓冲字符字节输入输出流:java.io.BufferedReaderjava.io.BufferedWriterjava.io.BufferedInputStreamjava.io.(代码片段

c++语言知识点汇总

分析C++软件异常需要掌握的汇编知识汇总(实战经验分享)

android小知识点代码片段