Java 字節流數據拷貝性能測試

 

package cn.edu.lcudcc.byte_buffer_time;

import java.io.*;

public class ByteBufferTimeDemo {
    private static final String SRC_FILE = "/Users/baoshan/Downloads/線上教學截圖/meeting_01.mp4";
    private static final String DEST_FILE = "/Users/baoshan/Downloads/";

    public static void main(String[] args) {
//        copy01(); // 低級 字節流複製
        copy02(); // 低級 字節數組流複製
//        copy03(); // 緩衝 字節流複製
        copy04(); // 緩衝 字節數組流複製  效率高,優先使用
    }

    private static void copy01() {
        long startTime = System.currentTimeMillis();
        try{
            InputStream is = new FileInputStream(SRC_FILE);
            OutputStream os = new FileOutputStream(DEST_FILE+"video1.mp4");

            int b;
            while ((b = is.read()) != -1) {
               os.write(b);
            }
            os.flush();
            os.close();
            is.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("使用低級的字節流按照一個一個字節的形式複製文件耗時:" + (endTime - startTime) / 1000.0 + "s");
    }

    private static void copy02() {
        long startTime = System.currentTimeMillis();
        try {
            InputStream is = new FileInputStream(SRC_FILE);
            OutputStream os = new FileOutputStream(DEST_FILE + "video2.mp4");
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1){
                os.write(buffer, 0, len);
            }
            os.flush();
            os.close();
            is.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("使用低級的字節流數組按照一個一個字節的形式複製文件耗時:" + (endTime - startTime) / 1000.0 + "s");
    }

    private static void copy03() {
        long startTime = System.currentTimeMillis();
        try{
            InputStream is = new FileInputStream(SRC_FILE);
            BufferedInputStream bis = new BufferedInputStream(is);
            OutputStream os = new FileOutputStream(DEST_FILE+"video3.mp4");
            BufferedOutputStream bos = new BufferedOutputStream(os);

            int b;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }
            bos.flush();
            bos.close();
            bis.close();
            os.close();
            is.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("使用緩衝的字節流按照一個一個字節的形式複製文件耗時:" + (endTime - startTime) / 1000.0 + "s");
    }

    private static void copy04() {
        long startTime = System.currentTimeMillis();
        try {
            InputStream is = new FileInputStream(SRC_FILE);
            BufferedInputStream bis = new BufferedInputStream(is);
            OutputStream os = new FileOutputStream(DEST_FILE+"video4.mp4");
            BufferedOutputStream bos = new BufferedOutputStream(os);

            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1){
                bos.write(buffer, 0, len);
            }
            bos.flush();
            bos.close();
            bis.close();
            os.close();
            is.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("使用緩衝的字節流數組按照一個一個字節的形式複製文件耗時:" + (endTime - startTime) / 1000.0 + "s");
    }

}

  

結論:高級緩衝字節數組流複製,效率最高,優先使用。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章