1. 程式人生 > 其它 >位元組流複製視訊的時間(基礎)(聯絡)

位元組流複製視訊的時間(基礎)(聯絡)

技術標籤:java

在這裡插入圖片描述

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秒

結論:
緩衝位元組流 > 基本位元組流
一次讀取一個位元組陣列 > 一次讀取一個位元組