Day 58

_字节缓冲流

  1. BufferOutputStream:该类实现缓冲输出流。通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入的每个字节导致系统底层的调用

  2. BufferOutputStream:创建BufferOutputStream将创建一个内部缓冲区数组。当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次很多字节

  3. 构造方法:

    • 字节缓冲输出流:BufferedOutStream(OutputStream out)
    • 字节缓冲输入流:BufferedInputStream(InputStream in)
  4. 字节缓冲流仅仅提供缓冲区,而真正的读写数据还得依靠基本的字节流对象进行操作

  5. package demo13;
    
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class _IO_Buffered_01 {
        public static void main(String[] args) {
            /*
            构造方法:
                - 字节缓冲输出流:BufferedOutStream(OutputStream out)
                - 字节缓冲输入流:BufferedInputStream(InputStream in)
             */
            BufferedInputStream bis= null;
            try {
                bis = new BufferedInputStream(new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\note_01"));
                int len;
                byte[] bytes = new byte[1024];
                if (bis != null) {
                    while ((len = bis.read()) != -1) {
                        System.out.print((char) len);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    ============================================
    abcdefg
    Process finished with exit code 0
    ********************************************
    note_01
        abcdefg
    

_字节流复制视频

  1. 需求:用4种方法把一个视频文件复制到当前的模块下,比较程序运行的时间

  2. 思路 :

    • 根据数据源创建字节输入流对象
    • 根据目的地创建字节流入流对象
    • 读写数据,复制视频
    • 释放资源
  3. package demo13;
    
    import java.io.*;
    
    public class _IO_Buffered_02 {
        public static void main(String[] args) {
            /*
            需求:用4种方法把一个视频文件复制到当前的模块下,比较程序运行的时间
    
             思路 :
               - 根据数据源创建字节输入流对象
               - 根据目的地创建字节流入流对象
               - 读写数据,复制视频
               - 释放资源
    
             基本字节流一次读写一个字节:       程序运行耗时:59099毫秒
             基本字节流一次读写一个字节数组:    程序运行耗时:101毫秒
             字节缓冲流一次读写一个字节:       程序运行耗时:258毫秒
             字节缓冲流一次读写一个字节数组:    程序运行耗时:37毫秒
             */
    
            // 记录开始时间
            long startTime = System.currentTimeMillis();
    //        run_01();     //  程序运行耗时:59099毫秒
    //        run_02();     //  程序运行耗时:101毫秒
    //        run_03();     //  程序运行耗时:258毫秒
            run_04();     //  程序运行耗时:37毫秒
    
    
    
            // 记录程序结束时间
            long endTime = System.currentTimeMillis();
            System.out.println("程序运行耗时:"+(endTime-startTime)+"毫秒");
        }
    
        // 单独使用FileOut/InStream,一次读取一个字节
        public static void run_01() {
            FileOutputStream fos = null;
            FileInputStream fis = null;
            try {
                fis = new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\截图\\一斗.mp4");
                fos = new FileOutputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\一斗.mp4");
                if (fis != null) {
                    int read;
                    while ((read = fis.read()) != -1) {
                        fos.write(read);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            finally {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        // 单独使用FileOut/InStream,一次读取byte[1024]
        public static void run_02() {
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\截图\\一斗.mp4");
                fos = new FileOutputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\一斗.mp4");
                if (fis != null) {
                    int len;
                    byte[] bytes = new byte[1024];
                    while ((len = fis.read(bytes)) != -1) {
                        fos.write(bytes,0,len);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        // 使用字节缓冲流复制视频,但一次只读取一个字节
        public static void run_03() {
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {
                FileInputStream fis = new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\截图\\一斗.mp4");
                FileOutputStream fos = new FileOutputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\一斗.mp4");
                bis = new BufferedInputStream(fis);
                bos = new BufferedOutputStream(fos);
    
                if (bis != null) {
                    int read;
                    while ((read = bis.read()) != -1) {
                        bos.write(read);
                    }
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        // 使用字节缓冲流复制视频,一次读取byte[1024]
        public static void run_04() {
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {
                FileInputStream fis = new FileInputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\截图\\一斗.mp4");
                FileOutputStream fos = new FileOutputStream("C:\\Users\\Alvord\\Desktop\\markdown学习\\code\\studyProject\\src\\demo13\\一斗.mp4");
                bis = new BufferedInputStream(fis);
                bos = new BufferedOutputStream(fos);
    
                if (bis != null) {
                    int len;
                    byte[] bytes = new byte[1024];
                    while ((len = bis.read(bytes)) != -1) {
                        bos.write(bytes,0,len);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    ====================================
    程序运行耗时:37毫秒
    
    Process finished with exit code 0
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值