栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

一、缓冲流的产生背景

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

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

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

二、BufferedOutputStream的使用

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

2、构造方法:

                public BufferedOutputStream(OutputStream out)

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、使用案例:

	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.csdn.net/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的速度测试我就不搞出来了,在我的上一篇博客文章中有源代码,加入计时的语句就能够测出时间了,我就不在这里多搞一次了。拜拜。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1040197.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号