java 20 -10 字節流四種方式復制mp3文件,測試效率


電腦太渣,好慢。。反正速率是:

高效字節流一次讀寫一個字節數組  >  基本字節流一次讀寫一個字節數組  > 高效字節流一次讀寫一個字節 >  基本字節流一次讀寫一個字節

  前兩個遠遠快過后面2個

 1 package zl_IOdemo;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.FileInputStream;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileOutputStream;
 8 import java.io.IOException;
 9 
10 /*
11  * 需求:把D:\music\音樂\Beyond - 不再猶豫.mp3復制到當前項目目錄下的copy.mp4中
12  * 
13  * 字節流四種方式復制文件:
14  * 基本字節流一次讀寫一個字節:   
15  * 基本字節流一次讀寫一個字節數組: 
16  * 高效字節流一次讀寫一個字節: 
17  * 高效字節流一次讀寫一個字節數組: 
18  */
19 public class CopyMp4 {
20 
21     public static void main(String[] args) throws IOException {
22         long start = System.currentTimeMillis();
23         //分別針對四種方式各創建一個方法,
24         //參數列表:String 數據源  String 目的地
25         //返回類型 void
26         method1("D:\\music\\音樂\\Beyond - 不再猶豫.mp3","copy.mp3");
27         //method2("D:\\music\\音樂\\Beyond - 不再猶豫.mp3","copy.mp3");
28         //method3("D:\\music\\音樂\\Beyond - 不再猶豫.mp3","copy.mp3");
29         //method4("D:\\music\\音樂\\Beyond - 不再猶豫.mp3","copy.mp3");
30         long end = System.currentTimeMillis();
31         System.out.println(end);
32         System.out.println("一共耗時"+(end - start)+"毫秒");
33         
34     }
35 
36     private static void method4(String start , String end) throws IOException {
37         //高效字節流一次讀寫一個字節數組
38         BufferedInputStream in = new BufferedInputStream(new FileInputStream(start));
39         BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(end));
40         byte[] by = new byte[1024];
41         int lend = 0;
42         while((lend = in.read(by)) != -1){
43             out.write(by,0,lend);
44         }
45         in.close();
46         out.close();
47         
48     }
49 
50     private static void method3(String start , String end) throws IOException {
51         // 高效字節流一次讀寫一個字節
52         BufferedInputStream in = new BufferedInputStream(new FileInputStream(start));
53         BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(end));
54         int lend = 0;
55         while(( lend = in.read()) != -1){
56         out.write(lend);
57         }
58         in.close();
59         out.close();
60     }
61 
62     private static void method2(String start , String end) throws IOException {
63         // 基本字節流一次讀寫一個字節數組
64         FileInputStream in = new FileInputStream(start);
65         FileOutputStream out = new FileOutputStream(end);
66         
67         byte[] by = new byte[1024];
68         int lend = 0;
69         while((lend = in.read(by)) != -1){
70             out.write(by,0,lend);
71         }
72         in.close();
73         out.close();
74         
75     }
76 
77     private static void method1(String start , String end) throws IOException {
78         // 基本字節流一次讀寫一個字節
79         //創建基本字節輸入流,以便從數據源讀取文件
80         FileInputStream in = new FileInputStream(start);
81         //創建基本字節輸出流,以便寫入數據到目的地
82         FileOutputStream out = new FileOutputStream(end);
83         //復制目標文件
84         int i = 0;
85         while((i = in.read()) != -1){
86             out.write(i);
87         }
88         in.close();
89         out.close();
90         
91     }
92 
93 }

 


免責聲明!

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



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