自己亲手测试一下,看下各自所用的时间,一目了然。代码体现:
import java.io.*;
public class CopyTest {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
method();
method2();
long end = System.currentTimeMillis();
System.out.println("共用时:" + (end - start) + "毫秒");
}
//高效字节流一次读写一个字节数组
private static void method() 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 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();
}
|