Java基础—IO流(第五节)

字节缓冲流

1. 字节缓冲流介绍

在这里插入图片描述
Java缓冲流本身并不具有IO流的读取与写入功能,只是在别的流上加上缓冲功能来提高效率。缓冲流有三个特点:

  • 提升程序性能
  • 最底层一定是节点流
  • 释放资源时只释放最外层的就可以(也就是只释放缓冲流的即可)

当对文件或者其他数据源进行频繁的读写操作时,效率很低,但如果在节点流上加入缓冲流就能高效的提升读写效率。缓冲流是先将信息缓存起来,当缓存区存满后或者手动刷新时再一次性的读取到程序或写入目的地。

2. 具体代码

BufferedInputStream

public class BufferedTest01 {
    public static void main(String[] args) {
        //创建源
        File src = new File("src/test.txt");
        //选择流
        InputStream is = null;
        try{
            is = new BufferedInputStream(new FileInputStream(src));//套在需要提升性能的节点流上
            byte[] flush = new byte[1024];
            int len = -1;
            while ((len=is.read(flush))!=-1){
                String str = new String(flush,0,len);
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(null!=is){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 }

BufferedOutputStream

public class BufferedTest02 {
    public static void main(String[] args) {
        //创建源
        File dest = new File("src/test");
        //选择流
        OutputStream os = null;
        try{
            os = new BufferedOutputStream(new FileOutputStream(dest));//套在需要提升性能的节点流上
            //操作
            String msg = "I believe I can success";
            byte[] datas = msg.getBytes();
            os.write(datas,0,datas.length);
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(null!=os){
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

字符缓冲流

1. 字符缓冲流介绍

在这里插入图片描述

2. 具体代码

BufferedReader

public class BufferedTest03 {
    public static void main(String[] args) {
        //创建源
        File src = new File("src/IO_study02/abc");
        //选择流
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(src));//将缓冲流加入到需要提升性能的节点流上
            //操作
            String line = null;
            while ((line=reader.readLine())!=null){//新添readLine()功能。读一行文字
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(null!=reader){
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

BufferedWriter

public class BufferedTest04 {
    public static void main(String[] args) {
        //创建源
        File dest = new File("desk");
        //选择流
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(dest));//将缓冲流添加到需要提升性能的节点流上
            //操作
            writer.append("Everything is possible");
            writer.newLine();//新增newLine()功能。写一行行分隔符。
            writer.append("总有一天通过我自己的努力,会得到我想要得到的一切");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(null!=writer){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值