Java基础进阶IO流-BufferReader,BufferWriter字符缓冲流

导读:本篇文章讲解 Java基础进阶IO流-BufferReader,BufferWriter字符缓冲流,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

BufferedReader:

带有缓冲区的字符输入流。
使用这个流的时候不需要自定义char数组,或者说不需要自定义byte数组。自带缓冲。

当一个流的构造方法中需要一个流的时候,这个被传进来的流叫做:节点流。
外部负责包装的这个流,叫做:包装流,还有一个名字叫做:处理流。

示例代码01:

public class BufferReaderTest01 {
    public static void main(String[] args) {

        BufferedReader br = null;
        try {
            FileReader reader = new FileReader("Copy02Test01.java");
            // 当一个流的构造方法中需要一个流的时候,这个被传进来的流叫做:节点流。
            // 外部负责包装的这个流,叫做:包装流,还有一个名字叫做:处理流。
            // 像当前这个程序来说:FileReader就是一个节点流。BufferedReader就是包装流/处理流。
            br = new BufferedReader(reader);

            /*//读一行
            String line1 = br.readLine();
            System.out.println(line1);
            //读第二行
            String line2 = br.readLine();
            System.out.println(line2);
            //读第三行
            String line3 = br.readLine();
            System.out.println(line3);*/

            //循环读取全部内容,API当s为null时已读完
            String s = null;//String类型默认值为null
            while((s = br.readLine()) != null){
                System.out.println(s);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(br != null){
                try {
                    // 关闭流
                    // 对于包装流来说,只需要关闭最外层流就行,里面的节点流会自动关闭。(可以看源代码。)
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

运行结果:

在这里插入图片描述

转换流:InputStreamReader

示例代码02:

public class BufferReaderTest02 {
    public static void main(String[] args) {

        BufferedReader br = null;
        try {
           /* //字节输入流
            FileInputStream in = new FileInputStream("Copy02Test01.java");

            //字符转换流,把字节流转换成字符流
            //这里的in是节点流,reader是包装流,节点流和包装流是相对的
            InputStreamReader reader = new InputStreamReader(in);

            //字符缓冲流
            // 这个构造方法只能传一个字符流。不能传字节流。
            //这里的reader是节点流,br则是包装流
            br = new BufferedReader(reader);*/

            //合并格式
            br = new BufferedReader(new InputStreamReader(new FileInputStream("Copy02Test01.java")));

            //读取数据
            String s = null;
            while((s = br.readLine()) != null){
                System.out.println(s);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(br != null){
                try {
                    //关闭最外层
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

运行结果:

在这里插入图片描述

BufferedWriter:带有缓冲的字符输出流。OutputStreamWriter:转换流

示例代码03:

public class BufferWriterTest {
    public static void main(String[] args) {
        BufferedWriter out = null;
        try {
            // 带有缓冲区的字符输出流
            //out = new BufferedWriter(new FileWriter("temp6.txt"));
            //转换流格式

            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("temp6.txt",true)));
            //写
            out.write("helloworld!");
            out.write("\n");
            out.write("helllokitty!");
            //刷新
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/94254.html

(0)
小半的头像小半

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!