文件复制测试流的性能
package com.test.test02;
import java.io.*;
//文件拷贝,高效字节流测试(文件大小:5.24M)
public class Test01 {
public static void main(String[] args) {
copy01();
// copy02();
}
private static void copy02() {
//基本流拷贝,一个字节一个字节拷贝
long startTime = System.currentTimeMillis();
System.out.println("基本流开始复制了");
try (
FileInputStream fis = new FileInputStream("E:/copyTest/八卦.pdf");
FileOutputStream fos = new FileOutputStream("E:/targetPath/八卦test.pdf")
) {
//读
int len;
while ((len = fis.read()) != -1) {
//写
fos.write(len);
}
//数组
/*byte[] bytes = new byte[1024];
int len;
while ((len=fis.read(bytes))!=-1){
fos.write(bytes);
}*/
} catch (IOException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("基本流结束复制了");
System.out.println("基本流耗时:" + (endTime - startTime));
/*
数组:438
一个一个字节复制:60152
*/
}
private static void copy01() {
//高效流复制文件
long startTime = System.currentTimeMillis();
System.out.println("增强流开始复制了");
try (
BufferedInputStream bis =
new BufferedInputStream(new FileInputStream("E:/copyTest/八卦.pdf"));
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream("E:/targetPath/八卦test.pdf"))
) {
/*byte[] bytes=new byte[1024];
int len;
while ((len=bis.read(bytes))!=-1){
bos.write(bytes);
}*/
int len;
while ((len=bis.read())!=-1){
bos.write(len);
}
} catch (IOException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("增强流结束复制了");
System.out.println("增强流耗时:" + (endTime - startTime));
/*
数组:30
一个一个字节复制:369
*/
}
}
唯有努力,人生没有白走的路,每一步都算数

浙公网安备 33010602011771号