缓冲字节流(BufferedInputStream、BufferedOutputStream)

BufferedInputStream 输入流,字节流,处理流BufferedOutputStream 输出流,字节流,处理流

缓冲字节流是对其他字节流的包装,不直接读写数据源设备缓冲流的工作原理如下:

import java.io.*;
/*
// 分别测试四种方式的效率
1.一次读写一个字节  ③
2.一次读写一个字节数组 ①
3.缓冲流一次读写一个字节  ②
4.缓冲流一次读写一个字节数组 ①   优先使用这种
 */
public class Test {
    public static void main(String[] args) throws IOException {
//        copyFile01();
//        copyFile02();
//        copyFile03();
        copyFile04();

    }

    // 一次读写一个字节:109s
    public static void copyFile01() throws IOException{
        FileInputStream fis = new FileInputStream("D:\\course\\资料\\nwt_setup_3.4.3045.exe");
        FileOutputStream fos = new FileOutputStream("day09\\nwt_setup_3.4.3045.exe");
        long start = System.currentTimeMillis();  // 获取当前时间

        int by;
        while ((by = fis.read()) != -1){
            fos.write(by);
        }

        long end = System.currentTimeMillis();
        System.out.println("耗时:" + (end - start) + "ms");

        fis.close();
        fos.close();
    }

    // 8192字节数组:46ms 、1024字节数组:183ms
    public static void copyFile02() throws IOException{
        FileInputStream fis = new FileInputStream("D:\\course\\资料\\nwt_setup_3.4.3045.exe");
        FileOutputStream fos = new FileOutputStream("day09\\nwt_setup_3.4.3045.exe");
        long start = System.currentTimeMillis();  // 获取当前时间

        int len;
        byte[] bytes = new byte[1024];  // 定义数组
        while ((len = fis.read(bytes)) != -1){
            fos.write(bytes,0,len);
        }

        long end = System.currentTimeMillis();
        System.out.println("耗时:" + (end - start) + "ms");

        fis.close();
        fos.close();
    }

    // 时间894ms  缓冲区大小默认8192字节
    public static void copyFile03() throws IOException{
        FileInputStream fis = new FileInputStream("D:\\course\\资料\\nwt_setup_3.4.3045.exe");
        BufferedInputStream bis = new BufferedInputStream(fis);

        FileOutputStream fos = new FileOutputStream("day09\\nwt_setup_3.4.3045.exe");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        long start = System.currentTimeMillis();  // 获取当前时间

        int by;
        while ( (by = bis.read()) != -1){
            bos.write(by);
        }

        long end = System.currentTimeMillis();
        System.out.println("时间:" + (end - start) + "ms");

//        bos.flush();  // 手动刷新缓冲区
        bis.close(); // 关闭此输入流并释放与该流关联的所有系统资源
        bos.close(); // 将缓冲区中最后一次没有满的数据,手动刷新到文件中

    }

    // 1024字节数组 + 8192缓冲区  62ms   /  8192字节数组 + 8192缓冲区  45 - 55ms之间
   public static void copyFile04() throws IOException{
       FileInputStream fis = new FileInputStream("D:\\course\\资料\\nwt_setup_3.4.3045.exe");
       BufferedInputStream bis = new BufferedInputStream(fis);

       FileOutputStream fos = new FileOutputStream("day09\\nwt_setup_3.4.3045.exe");
       BufferedOutputStream bos = new BufferedOutputStream(fos);

       long start = System.currentTimeMillis(); // 获取当前时间
       
       int len;
       byte[] bytes = new byte[8192]; // 定义数组

       while ((len = bis.read(bytes)) != -1){
            bos.write(bytes,0,len);
       }
       long end = System.currentTimeMillis();

       System.out.println("时间:" + (end - start) + "ms");

        bis.close();
        bos.close();
   }
}

 

posted @ 2022-05-22 21:41  鹿先森JIAN  阅读(534)  评论(0编辑  收藏  举报