java 20 -10 字节流四种方式复制mp3文件,测试效率

时间:2024-08-04 14:05:32

电脑太渣,好慢。。反正速率是:

高效字节流一次读写一个字节数组  >  基本字节流一次读写一个字节数组  > 高效字节流一次读写一个字节 >  基本字节流一次读写一个字节

  前两个远远快过后面2个
 package zl_IOdemo;

 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; /*
* 需求:把D:\music\音乐\Beyond - 不再犹豫.mp3复制到当前项目目录下的copy.mp4中
*
* 字节流四种方式复制文件:
* 基本字节流一次读写一个字节:
* 基本字节流一次读写一个字节数组:
* 高效字节流一次读写一个字节:
* 高效字节流一次读写一个字节数组:
*/
public class CopyMp4 { public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
//分别针对四种方式各创建一个方法,
//参数列表:String 数据源 String 目的地
//返回类型 void
method1("D:\\music\\音乐\\Beyond - 不再犹豫.mp3","copy.mp3");
//method2("D:\\music\\音乐\\Beyond - 不再犹豫.mp3","copy.mp3");
//method3("D:\\music\\音乐\\Beyond - 不再犹豫.mp3","copy.mp3");
//method4("D:\\music\\音乐\\Beyond - 不再犹豫.mp3","copy.mp3");
long end = System.currentTimeMillis();
System.out.println(end);
System.out.println("一共耗时"+(end - start)+"毫秒"); } private static void method4(String start , String end) throws IOException {
//高效字节流一次读写一个字节数组
BufferedInputStream in = new BufferedInputStream(new FileInputStream(start));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(end));
byte[] by = new byte[1024];
int lend = 0;
while((lend = in.read(by)) != -1){
out.write(by,0,lend);
}
in.close();
out.close(); } private static void method3(String start , String end) throws IOException {
// 高效字节流一次读写一个字节
BufferedInputStream in = new BufferedInputStream(new FileInputStream(start));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(end));
int lend = 0;
while(( lend = in.read()) != -1){
out.write(lend);
}
in.close();
out.close();
} private static void method2(String start , String end) throws IOException {
// 基本字节流一次读写一个字节数组
FileInputStream in = new FileInputStream(start);
FileOutputStream out = new FileOutputStream(end); byte[] by = new byte[1024];
int lend = 0;
while((lend = in.read(by)) != -1){
out.write(by,0,lend);
}
in.close();
out.close(); } private static void method1(String start , String end) throws IOException {
// 基本字节流一次读写一个字节
//创建基本字节输入流,以便从数据源读取文件
FileInputStream in = new FileInputStream(start);
//创建基本字节输出流,以便写入数据到目的地
FileOutputStream out = new FileOutputStream(end);
//复制目标文件
int i = 0;
while((i = in.read()) != -1){
out.write(i);
}
in.close();
out.close(); } }