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 }

 

posted @ 2016-09-30 23:20  卡拉瓦  阅读(2162)  评论(0编辑  收藏  举报