Java进阶(三十八)IO流6:缓冲流

本文介绍了如何使用Java的BufferedInputStream和BufferedOutputStream加速文件复制,通过实例展示了它们在字节和字符流中的使用,以及与节点流的性能对比,节省了大量时间。
摘要由CSDN通过智能技术生成

一、概述

1.1缓冲流

  • 缓冲流为处理流的一种,分别对4个节点流进行了包装处理。
节点流缓冲流
FileInputStreamBufferedInputStream
FileOutputStreamBufferedOutputStream
FileReaderBufferedReader
FileWriterBufferedWriter

1.2作用

  • 提高流的读取、写入的速度。
  • 原因:内部提供了一个缓冲区 byte buf[8192]。

二、BufferedInputStream、BufferedOutputStream(字节型)

2.1步骤

  1. 实例化File类对象。
  2. 实例化节点流对象FileInputStreamFileOutputStream
  3. 用节点流对象再实例化缓冲流对象BufferedInputStreamBufferedOutputStream
  4. 使用byte数组、缓冲流进行读写。
  5. 关闭流资源。

2.2要求与注意

  • 流资源关闭时,应先关闭外层资源,再关闭内存资源。即先关闭缓冲流,再关闭节点流。
  • 但由于关闭外层流时,内层流也会自动进行关闭,故内存流的关闭可以省略不写。
    /**
     * 实现非文本文件的复制
     */
    @Test
    public void BufferedStreamTest() {

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //1.造文件
            File srcFile = new File("photomode.png");
            File destFile = new File("photomode2.png");

            //2.造流
            //2.1造节点流
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);

            //2.2造缓冲流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            //3.复制的细节:读取、写入
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            //4.关闭资源
            //要求:先关闭外层的流,再关闭内层的流
            try {
                if (bos != null)
                    bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bis != null)
                    bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //说明:关闭外层流的同时,内层流也会自动的进行关闭。关于内层流的关闭,可以省略。
//        fos.close();
//        fis.close();
        }
    }

2.3缓冲流与节点流读写速度对比

    //实现非文本文件的复制
    public void copyFileWithBuffered(String srcPath, String destPath ){

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //1.造文件
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);

            //2.造流
            //2.1造节点流
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);

            //2.2造缓冲流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);


            //3.复制的细节:读取、写入
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭资源
            //要求:先关闭外层的流,再关闭内层的流
            try {
                if (bos != null)
                    bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bis != null)
                    bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void testCopyFileWithBuffered(){
        long start = System.currentTimeMillis();

        String srcPath="E:\\developer\\idea\\workspace\\JavaSenior\\09.IO\\01-video.mp4";
        String destPath="E:\\developer\\idea\\workspace\\JavaSenior\\09.IO\\02-video.mp4";

        copyFileWithBuffered(srcPath,destPath);

        long end = System.currentTimeMillis();
        System.out.println("复制操作花费的时间:" + (end - start));//416

    }

 使用节点流复制花费的时间为1477毫秒,而使用缓冲流复制只需416毫秒。


三、BufferedReader、BufferedWriter(字符型)

2.1步骤

  1. 实例化File类对象。
  2. 实例化节点流对象FileReaderFileWriter
  3. 用节点流对象再实例化缓冲流对象BufferedReaderBufferedWriter
  4. 使用char数组、缓冲流进行读写。
  5. 关闭流资源。
    /**
     * 使用BufferedReader和BufferedWriter实现文本文件的复制
     */
    @Test
    public void testBufferedReaderBufferedWriter() {
        //1.造文件
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            File srcFile = new File("hello.txt");
            File destFile = new File("hello2.txt");
            //2.造流
            //2.1节点流
            FileReader fr = new FileReader(srcFile);
            FileWriter fw = new FileWriter(destFile);
            //2.2缓冲流
            br = new BufferedReader(fr);
            bw = new BufferedWriter(fw);

            //3.读写数据
            char[] cbuffer = new char[1024];
            int len;
            while ((len = br.read(cbuffer)) != -1) {
                bw.write(cbuffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭流
            try {
                if (bw != null)
                    bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值