字节流复制视频的时间(基础)(练习)

在这里插入图片描述

public class CopyAviDemo {
    public static void main(String[] args) throws IOException {
        long startTime01 = System.currentTimeMillis();
        method01();
        long endTime01 = System.currentTimeMillis();
        System.out.println("1.耗时:" + (endTime01 - startTime01) + "秒");
        long startTime02 = System.currentTimeMillis();
        method02();
        long endTime02 = System.currentTimeMillis();
        System.out.println("2.耗时:" + (endTime02 - startTime02) + "秒");
        long startTime03 = System.currentTimeMillis();
        method03();
        long endTime03 = System.currentTimeMillis();
        System.out.println("3.耗时:" + (endTime03 - startTime03) + "秒");
        long startTime04 = System.currentTimeMillis();
        method04();
        long endTime04 = System.currentTimeMillis();
        System.out.println("4.耗时:" + (endTime04 - startTime04) + "秒");
    }

    public static void method01() throws IOException {
        FileInputStream fis = new FileInputStream("E:\\练习用\\cc01.avi");
        FileOutputStream fos = new FileOutputStream("com.idea_primary\\字节流字节复制.avi");
        int i;
        while ((i = fis.read()) != -1) {
            fos.write(i);
        }
        fis.close();
        fos.close();
    }
    public static void method02() throws IOException {
        FileInputStream fis = new FileInputStream("E:\\练习用\\cc02.avi");
        FileOutputStream fos = new FileOutputStream("com.idea_primary\\字节流字节数组复制.avi");
        int i;
        byte[] b = new byte[1024];
        while ((i = fis.read(b)) != -1) {
            fos.write(b,0,i);
        }
        fis.close();
        fos.close();
    }
    public static void method03() throws IOException {
        FileInputStream fis = new FileInputStream("E:\\练习用\\cc03.avi");
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("com.idea_primary\\缓冲流字节复制.avi"));
        int i;
        while ((i = bis.read()) != -1) {
            bos.write(i);
        }
        bis.close();
        bos.close();
    }
    public static void method04() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\练习用\\cc01.avi"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("com.idea_primary\\缓冲流字节复制.avi"));
        byte[] b = new byte[1024];
        int i;
        while ((i = bis.read(b)) != -1) {
            bos.write(b,0,i);
        }
        bis.close();
        bos.close();
    }
}

输出台输出:
1.耗时:33525秒
2.耗时:120秒
3.耗时:256秒
4.耗时:17秒

结论:
缓冲字节流 > 基本字节流
一次读取一个字节数组 > 一次读取一个字节

posted @ 2021-02-18 18:39  丰北丰  阅读(139)  评论(0)    收藏  举报