1、使用字节流逐个读取并输出
2、使用字节流+数组进行读取输出
3、使用字节缓冲流逐个读取输出
4、使用字节缓冲流+数组读取输出
实验所用文件为Navicat.zip 大小为234.4 MB
package java_20220218.work.work_2;
import java.io.*;
/**
* @ClassName Work_2
* @Description 编写出你认为文件复制操作性能较高的案例。
* @Author chenxys
* @Date 2022/2/18 17:48
* @Version
*/
public class Work_2 {
static String path = "/Users/chenys/Desktop/test/Navicat.zip";
static String pathCopy = "/Users/chenys/Desktop/test/Navicat";
static long startTime;//存放开始时间
static long endTime;//存放结束时间
static int res = 0;//存放读取到到数据
public static void main(String[] args) throws IOException {
//字符流不能用于视频的读取和复制
fileInputStream_1("1.zip");
fileInputStream_2("2.zip");
fileInputStream_3("3.zip");
fileInputStream_4("4.zip");
}
//对比
//1、使用字节流逐个读取
public static void fileInputStream_1(String pp) throws IOException {
//创建字节流对象
InputStream in = new FileInputStream(path);
OutputStream out = new FileOutputStream(pathCopy+pp);//拼接目标文件
//1、逐个读取
startTime = System.currentTimeMillis();//将开始时间设为当前时间
while ((res=in.read()) != -1 ){//对文件进行读取,如果读完则返回-1
out.write(res);//将读取到到写入到目标文件
}
endTime = System.currentTimeMillis();//结束时间
//关闭源
in.close();
out.close();
System.out.println("使用字节流逐个读取花费时间:"+(endTime-startTime)+"毫秒");
}
//2、利用字节数组读取
public static void fileInputStream_2(String pp) throws IOException {
//创建字节流对象
InputStream in = new FileInputStream(path);
OutputStream out = new FileOutputStream(pathCopy+pp);//拼接目标文件
byte[] buff = new byte[1024];//声明字节数组
startTime = System.currentTimeMillis();//将开始时间设为当前时间
while ((res=in.read(buff)) != -1 ){//将读取到到数据存入数组
out.write(buff,0,res);//将数组中到数据写入目标文件
}
endTime = System.currentTimeMillis();//结束时间
//关闭源
in.close();
out.close();
System.out.println("使用字节流数组读取花费时间:"+(endTime-startTime)+"毫秒");
}
//3、利用缓冲字节流数组读取
public static void fileInputStream_3(String pp) throws IOException {
//创建字节流对象
FileInputStream in = new FileInputStream(path);
FileOutputStream out = new FileOutputStream(pathCopy+pp);//拼接目标文件
//创建缓冲字节流对象
BufferedInputStream inBuff = new BufferedInputStream(in);
BufferedOutputStream outBuff = new BufferedOutputStream(out);
byte[] buff = new byte[1024];//声明字节数组
startTime = System.currentTimeMillis();//开始时间为当前系统时间
while ((res=inBuff.read(buff)) != -1 ){//将读取到到数据存入字节数组
outBuff.write(buff,0,res);//写入目标文件
}
outBuff.flush();
endTime = System.currentTimeMillis();//结束时间
//关闭源
in.close();
out.close();
inBuff.close();
outBuff.close();
System.out.println("使用字节缓冲流数组读取花费时间:"+(endTime-startTime)+"毫秒");
}
//4、利用缓冲字节流逐个读取
public static void fileInputStream_4(String pp) throws IOException {
//创建字节流对象
FileInputStream in = new FileInputStream(path);
FileOutputStream out = new FileOutputStream(pathCopy+pp);//拼接目标文件
//创建字节缓冲流对象
BufferedInputStream inBuff = new BufferedInputStream(in);
BufferedOutputStream outBuff = new BufferedOutputStream(out);
//逐个读取
startTime = System.currentTimeMillis();//开始时间:当前系统时间
while ((res=inBuff.read()) != -1 ){//逐个读取
outBuff.write(res);//写入目标文件
}
outBuff.flush();//刷新至持久层
endTime = System.currentTimeMillis();//结束时间
//关闭源
in.close();
out.close();
inBuff.close();
outBuff.close();
System.out.println("使用字节缓冲流逐个读取花费时间:"+(endTime-startTime)+"毫秒");
}
}
1、使用字节流逐个读取花费时间:632000毫秒
2、使用字节流数组读取花费时间:870毫秒
3、使用字节缓冲流数组读取花费时间:236毫秒
4、使用字节缓冲流逐个读取花费时间:2556毫秒
结果显而易见:
1、使用字节缓冲流+数组复制文件比使用字节流+数组快
2、使用字节缓冲流逐个读取比使用字节流逐个读取快