io流(缓冲字节流)

缓冲字节流(BufferInputStream,BufferOutputStream)

  • 我们之前使用文件字节流,有两种使用方案(一个高效,一个低效),但他们的操作还是不够快,所以就引入我们今天的缓冲字节流

    它们的大致运行图如下:

    图片

  • 缓冲字节流实例代码如下:

package com.bjsxt.test03;

import java.io.*;

public class Test03 {
    public static void main(String[] args) throws IOException {
        //1.确定源文件
        File f1 = new File("D:\\a\\b.txt");
        //2.确定目标文件
        File f2 = new File("D:\\a\\a.txt");
        //字节流站在工作的第一线,直接跟源文件或目标文件接触
        FileInputStream fis = new FileInputStream(f1);
        FileOutputStream fos = new FileOutputStream(f2);
        //字节流外面包着缓冲字节流
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //开始动作
        byte[] b = new byte[4];
        int n = bis.read(b);
        //记录时间
        long startTime = System.currentTimeMillis();
        while(n!=-1){
            bos.write(b,0,n);
            n=bis.read(b);
        }
        long endTime = System.currentTimeMillis();
        System.out.println((endTime-startTime)+"毫秒");
        //关闭流:其实只关闭高级流就可以了
        bos.close();
        bis.close();
        fos.close();
        fis.close();
    }
}

缓冲字节流就是将一根根套在另一根管上(文件字节流),就可以将内容一次性读取到那跟管上

posted on   汪汪程序员  阅读(149)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示