案例:復制視頻(四種方式復制)


 

 四種方式實現復制視頻,並記錄每種方式復制視頻的時間

  1. 基本字節流一次讀寫一個字節
  2. 基本字節流一次讀寫一個字節數組
  3. 字節緩沖流一次讀寫一個字節
  4. 字節緩沖流一次讀寫一個字節數組

 

public class CopyAviDemo {
    public static void main(String[] args) throws IOException{
        //記錄開始時間
        long startTime = System.currentTimeMillis();

        //復制視頻
//        method1();      //共耗時:454毫秒
//        method2();      //共耗時:2毫秒
//        method3();      //共耗時:4毫秒
        method4();      //共耗時:1毫秒
        //記錄結束時間
        long endTime = System.currentTimeMillis();

        //記錄耗時時間
        System.out.println("共耗時:" + (endTime-startTime)+"毫秒");
    }
    //字節緩沖流一次讀寫一個字節數組
    public static void method4()throws IOException {
        //創建對象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\java\\復制視頻案例.mp4"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myFile\\復制視頻案例.mp4"));

        byte[] bys = new byte[1024];
        int len;
        while ((len=bis.read(bys))!=-1){
            bos.write(bys,0,len);
        }
        bis.close();
        bos.close();
    }
    //字節緩沖流一次讀寫一個字節
    public static void method3()throws IOException {
        //創建對象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\java\\復制視頻案例.mp4"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myFile\\復制視頻案例.mp4"));

        //讀寫數據
        int by;
        while ((by=bis.read())!=-1){
            bos.write(by);
        }
        bis.close();
        bos.close();
    }
    //基本字節流一次讀寫一個字節數組
    public static void method2()throws IOException {
        //創建對象
        FileInputStream fis = new FileInputStream("F:\\java\\復制視頻案例.mp4");
        FileOutputStream fos = new FileOutputStream("myFile\\復制視頻案例.mp4");

        byte[] bys = new byte[1024];
        int len;
        while ((len=fis.read(bys))!=-1){
            fos.write(bys,0,len);
        }
        fis.close();
        fos.close();
    }
    //基本字節流一次讀寫一個字節
    public static void method1()throws IOException {
        //創建對象
        FileInputStream fis = new FileInputStream("F:\\java\\復制視頻案例.mp4");
        FileOutputStream fos = new FileOutputStream("myFile\\復制視頻案例.mp4");

        //讀寫數據
        int by;
        while ((by=fis.read())!=-1){
            fos.write(by);
        }

        //釋放資源
        fis.close();
        fos.close();
    }
}

選的視頻文件有點太小了,導致差別不明顯。

使用字節緩沖流一次讀寫一個字節數組的速度是最快的

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM