package cn.itcast.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 字节流复制数据:
* A:基本字节流一次读写一个字节 44813毫秒
* B:基本字节流一次读写一个字节数组 78毫秒
* C:高效字节流一次读写一个字节 438毫秒
* D:高效字节流一次读写一个字节数组 16毫秒
*
* 测试时间
*/
public class CopyMP3Test {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
// method1();
// method2();
// method3();
method4();
long end = System.currentTimeMillis();
System.out.println("供耗时:" + (end - start) + "毫秒");
}
//高效字节流一次读写一个字节数组
private static void method4() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
"d:\\zxmzf.mp3"));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("4.mp3"));
byte[] bys = new byte[1024];
int len = 0;
while((len=bis.read(bys))!=-1){
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
// 高效字节流一次读写一个字节
private static void method3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
"d:\\zxmzf.mp3"));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("3.mp3"));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bis.close();
bos.close();
}
// 基本字节流一次读写一个字节数组
private static void method2() throws IOException {
FileInputStream fis = new FileInputStream("d:\\zxmzf.mp3");
FileOutputStream fos = new FileOutputStream("2.mp3");
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
// 基本字节流一次读写一个字节
private static void method1() throws IOException {
FileInputStream fis = new FileInputStream("d:\\zxmzf.mp3");
FileOutputStream fos = new FileOutputStream("1.mp3");
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
}
|