复制视频四种实现方式(java)

package com.company;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class BufferedStreamDemo {
    public static void main(String[] args) throws IOException {
        //复制视频 四种方式
        long starttime = System.currentTimeMillis();
//        method1();  //字节流单字节复制   3049ms
//        method2(); //字节流字符数组   4ms
//        method3();  //字节缓冲流单字节  17ms
//        method4();  //字节缓冲里字符数组  2ms
        long endtime = System.currentTimeMillis();
        System.out.println("耗时:"+(endtime-starttime)+"ms");
    }
    public static void method1() throws IOException {
        FileInputStream fls = new FileInputStream("F:\\test.mp4");
        FileOutputStream fos = new FileOutputStream("test.mp4");
        int len;
        while ((len= fls.read())!=-1){
            fos.write(len);
        }
        fls.close();
        fos.close();
    }
    public static void method2() throws IOException {
        FileInputStream fls = new FileInputStream("F:\\test.mp4");
        FileOutputStream fos = new FileOutputStream("test1.mp4");
        byte[] b = new byte[1024];
        int len;
        while ((len= fls.read(b))!=-1){
            fos.write(b,0,len);
        }
        fls.close();
        fos.close();
    }
    public static void method3() throws IOException {
        FileInputStream fls = new FileInputStream("F:\\test.mp4");
        FileOutputStream fos = new FileOutputStream("test2.mp4");
        BufferedInputStream bis = new BufferedInputStream(fls);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int len;
        while ((len= bis.read())!=-1){
            bos.write(len);
        }
        bis.close();
        bos.close();
    }
    public static void method4() throws IOException {
        FileInputStream fls = new FileInputStream("F:\\test.mp4");
        FileOutputStream fos = new FileOutputStream("test3.mp4");
        BufferedInputStream bis = new BufferedInputStream(fls);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        byte[] b = new byte[1024];
        int len;
        while ((len= bis.read(b))!=-1){
            bos.write(b,0,len);
        }
        bis.close();
        bos.close();
    }
}

你可能感兴趣的:(java)