天天看點

位元組流複制視訊(基本流和緩沖流 對比)

1 :根據資料源建立位元組輸入流對象
	2 :根據目的地建立位元組輸出流對象
	3:讀寫資料,複制圖檔(-次讀取一個位元組數組,-次寫入一個位元組數組)
	4:釋放資源
           

四種方式實作複制視訊,并記錄每種方式複制視訊的時間

1:基本位元組流一-次讀寫一個位元組

2:基本位元組流一次讀寫- 個位元組數組

3:位元組緩沖流一次讀寫一個位元組

4:位元組緩沖流一次讀寫一個位元組數組

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

        //複制視訊
//        method1();         //共耗時41728毫秒
//        method2();        //共耗時59毫秒
//        method3();          //共耗時219毫秒
        method4();              //共耗時23毫秒

        //記錄結束時間
        long endtime = System.currentTimeMillis();

        System.out.println("共耗時"+(endtime-startime)+"毫秒");



    }

    //基本位元組流一-次讀寫一個位元組
    public static void method1() throws IOException {
//        1 :根據資料源建立位元組輸入流對象
        FileInputStream fis = new FileInputStream("C:\\Users\\PCTC\\Desktop\\file\\1.wmv");
        2 :根據目的地建立位元組輸出流對象
        FileOutputStream fos = new FileOutputStream("C:\\Users\\PCTC\\Desktop\\file\\JavaSE\\1.wmv");
        3:讀寫資料,複制圖檔(基本位元組流一-次讀寫一個位元組)
        int by;
        while ((by = fis.read()) !=-1){
            fos.write(by);
        }
        4:釋放資源
        fis.close();
        fos.close();

        //共耗時41728毫秒
    }


//基本位元組流一次讀寫- 個位元組數組
    public static void method2() throws IOException {
//        1 :根據資料源建立位元組輸入流對象
        FileInputStream fis = new FileInputStream("C:\\Users\\PCTC\\Desktop\\file\\1.wmv");
        2 :根據目的地建立位元組輸出流對象
        FileOutputStream fos = new FileOutputStream("C:\\Users\\PCTC\\Desktop\\file\\JavaSE\\1.wmv");
        3:讀寫資料,複制圖檔(基本位元組流一次讀寫- 個位元組數組)
        byte[] bys = new byte[1024];
        int len;
        while ((len = fis.read(bys)) !=-1){
            fos.write(bys,0,len);
        }
        4:釋放資源
        fis.close();
        fos.close();

        //共耗時59毫秒
    }

    //位元組緩沖流一次讀寫一個位元組
    public static void method3() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\PCTC\\Desktop\\file\\1.wmv"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\PCTC\\Desktop\\file\\JavaSE\\1.wmv"));
        int by;
        while ((by = bis.read()) != -1){
            bos.write(by);
        }
        bis.close();
        bos.close();
//共耗時219毫秒
    }

    //位元組緩沖流一次讀寫一個位元組數組
    public static void method4() throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\PCTC\\Desktop\\file\\1.wmv"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\PCTC\\Desktop\\file\\JavaSE\\1.wmv"));
        byte[] bys = new byte[1024];
        int len;
        while ((len = bis.read(bys)) != -1){
            bos.write(bys,0,len);
        }
        bis.close();
        bos.close();
//共耗時23毫秒
    }
}