BufferedOutputStream和BufferedInputStream字节缓冲流如何使用呢?

本文介绍了Java中为了提高IO流读写速度而引入的缓冲流,详细阐述了BufferedOutputStream和BufferedInputStream的使用方法及构造方法,并通过文件复制操作展示了它们的高效性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、缓冲流的产生背景

在我们学习字节流与字符流的时候,大家都进行过读取文件中数据的操作,读取数据量大的文件时,读取的速度会很慢,很影响我们程序的效率,那么,我想提高速度,怎么办?

Java中提高了一套缓冲流,它的存在,可提高IO流的读写速度

缓冲流,根据流的分类分类字节缓冲流与字符缓冲流。

二、BufferedOutputStream的使用

1、类的功能:高效进行文件的读写操作写数据到文件的操作

2、构造方法

                public BufferedOutputStream(OutputStream out)

3、使用案例:

/*
	 * 写数据到文件的方法
	 * 1,创建流
	 * 2,写数据
	 * 3,关闭流
	 */
	private static void write() throws IOException {
		//创建基本的字节输出流
		FileOutputStream fileOut = new FileOutputStream("abc.txt");
		//使用高效的流,把基本的流进行封装,实现速度的提升
		BufferedOutputStream out = new BufferedOutputStream(fileOut);
		//2,写数据
		out.write("hello".getBytes());
		//3,关闭流
		out.close();
	}

三、BufferedInputStream的使用

1、类的功能:高效完成读取文件中数据的操作。

2、构造方法:

                public BufferedInputStream(InputStream in)

3、使用案例:

/*
	 * 从文件中读取数据
	 * 1,创建缓冲流对象
	 * 2,读数据,打印
	 * 3,关闭
	 */
	private static void read() throws IOException {
		//1,创建缓冲流对象
		FileInputStream fileIn = new FileInputStream("abc.txt");
		//把基本的流包装成高效的流
		BufferedInputStream in = new BufferedInputStream(fileIn);
		//2,读数据
		int ch = -1;
		while ( (ch = in.read()) != -1 ) {
			//打印
			System.out.print((char)ch);
		}
		//3,关闭
		in.close();
	}

看到这里应该基本上就能够使用BufferedOutputStream流和BufferedInputStream流来进行高效的操作了,比如文件复制。

我的上一篇博客写了FileoutputStream和FileInputStream,那么BufferedOutputStream比FileoutputStream到底高效了多少倍呢?

如何使用FileInputStream和FileOutputStream实现对文件的复制操作呢?https://blog.youkuaiyun.com/JavaNewMans/article/details/126194362?spm=1001.2014.3001.5501

        我们这里也用BufferedStream来进行一次文件的复制操作!

四、高效流与基本流的速度比较——文件复制

import java.io.*;

public class BufferedStreamDemo1 {
    public static void main(String[] args) {
        // 文件对象
        File wordFile = new File("D:\\programming\\IDEA\\save\\docx\\03 虚拟仪器报告-实验三.doc");
        File photoFile = new File("D:\\programming\\IDEA\\save\\docx\\QQ图片20210901172834.jpg");
        File videoFile = new File("D:\\programming\\IDEA\\save\\docx\\VID_20210502_143040.mp4");

        // 文件复制
        wordCopy(wordFile);

        // 图片复制
        photoCopy(photoFile);

        // 视频复制
        videoCopy(videoFile);
    }

    public static void wordCopy(File file){
        // 开始计时
        long start = System.currentTimeMillis();

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        // 为复制出来的文件重命名
        String[] paths = file.getPath().split("\\.");
        String newPaths = paths[0]+"Copy."+paths[1];

        try {
            // 缓冲流对象
            bis = new BufferedInputStream(new FileInputStream(file));
            bos = new BufferedOutputStream(new FileOutputStream(newPaths));

            // 读数据
            int len=0;
            byte[] bytes = new byte[1024];
            while ((len=bis.read(bytes))!=-1){
                // 写数据
                bos.write(bytes,0,len);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                // 释放资源
                bis.close();
                bos.close();

                // 结束计时
                long end = System.currentTimeMillis();

                System.out.println("Buffered文件复制一共耗时:"+(end-start)+"毫秒");

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void photoCopy(File file){
        // 计时开始
        long start = System.currentTimeMillis();

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        // 为复制出来的文件重命名
        String[] paths = file.getPath().split("\\.");
        String newPath = paths[0]+"Copy."+paths[1];

        try {
            // 缓冲流对象
            bis = new BufferedInputStream(new FileInputStream(file));
            bos = new BufferedOutputStream(new FileOutputStream(newPath));

            // 读数据
            int len=0;
            byte[] bytes = new byte[1024];
            while ((len=bis.read(bytes))!=-1){
                // 写数据
                bos.write(bytes,0,len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                // 释放资源
                bis.close();
                bos.close();

                // 结束计时
                long end = System.currentTimeMillis();
                System.out.println("Buffered图片复制一共耗时:"+(end-start)+"毫秒");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void videoCopy(File file){
        // 计时开始
        long start = System.currentTimeMillis();

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        // 为复制出来的文件重命名
        String[] paths = file.getPath().split("\\.");
        String newPath = paths[0]+"Copy."+paths[1];

        try {
            // 缓冲流对象
            bis = new BufferedInputStream(new FileInputStream(file));
            bos = new BufferedOutputStream(new FileOutputStream(newPath));

            // 读数据
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len=bis.read(bytes))!=-1){
                // 写数据
                bos.write(bytes,0,len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                // 释放资源
                bis.close();
                bos.close();

                // 结束计时
                long end = System.currentTimeMillis();
                System.out.println("Buffered视频复制一共耗时:"+(end-start)+"毫秒");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果如下所示:

 

 

可以看出运行效果非常的快,其中图片复制仅用了1毫秒的时间,it is very fast!

而关于FileOutputStream和FileInputStream的速度测试我就不搞出来了,在我的上一篇博客文章中有源代码,加入计时的语句就能够测出时间了,我就不在这里多搞一次了。拜拜。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值