5种方法实现文件的拷贝(IO流)及比较效率
复习了一下IO流,简单的写了下几种文件拷贝,不是很严谨的比较了下他们之间的效率
package io_stream;
import org.junit.jupiter.api.Test;
import java.io.*;
import java.nio.channels.FileChannel;
/**
* @author Huakai
* @version 1.0
* @create 通过复制文件来对比字节流使用缓冲流的效率对比
* @since 2021/5/8 15:05
*/
public class TestCopy_03 {
// 简单使用字节流 且不使用缓冲数组
public void copy(File src, File des) throws IOException {
FileInputStream inputStream = new FileInputStream(src);
FileOutputStream outputStream = new FileOutputStream(des);
int len;
while ((len = inputStream.read()) != -1) {
outputStream.write(len);
}
outputStream.close();
// outputStream.flush();
inputStream.close();
}
// 简单使用字节流 且使用缓冲数组
public void copyOfArray(File src, File des) throws IOException {
FileInputStream inputStream = new FileInputStream(src);
FileOutputStream outputStream = new FileOutputStream(des);
int len;
byte[] bytes = new byte[1024];
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
outputStream.close();
inputStream.close();
}
// 简单使用处理流 且使用不缓冲数组
public void bufferedCopy(File src, File des) throws IOException {
FileInputStream inputStream = new FileInputStream(src);
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(des));
int len;
while ((len = inputStream.read()) != -1) {
outputStream.write(len);
}
outputStream.close();
inputStream.close();
}
// 简单使用处理流 且使用缓冲数组
public void bufferedCopyOfArray(File src, File des) throws IOException {
FileInputStream inputStream = new FileInputStream(src);
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(des));
int len;
byte[] bytes = new byte[1024];
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
outputStream.close();
inputStream.close();
}
/**
* FileChannel 文件通道拷贝
* jdk 1.7以前的写法
*/
public void channelCopy(File src, File des) {
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel fisChannel = null;
FileChannel fosChannel = null;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(des);
fisChannel = fis.getChannel();
fosChannel = fos.getChannel();
fisChannel.transferTo(0, fisChannel.size(), fosChannel);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fosChannel.close();
fisChannel.close();
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* FileChannel 文件通道拷贝
* jdk 1.8的的写法
*/
public void channelCopy1(File src, File des) {
try (FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(des);
FileChannel fisChannel = fis.getChannel();
FileChannel fosChannel = fos.getChannel();) {
fisChannel.transferTo(0, fisChannel.size(), fosChannel);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 比较不同方法的效率
*/
@Test
void test01() throws IOException {
String dir = "C:\\users\\huakai\\Desktop\\";
String srcName = "test";
String desName = "copy";
String suffix = ".7z";
File src = new File(dir, srcName + suffix);
File des = new File(dir, desName + suffix);
long start_time1 = System.currentTimeMillis();
copy(src, des);
long end_time1 = System.currentTimeMillis();
long start_time2 = System.currentTimeMillis();
bufferedCopy(src, des);
long end_time2 = System.currentTimeMillis();
long start_time3 = System.currentTimeMillis();
copyOfArray(src, des);
long end_time3 = System.currentTimeMillis();
long start_time4 = System.currentTimeMillis();
bufferedCopyOfArray(src, des);
long end_time4 = System.currentTimeMillis();
long start_time5 = System.currentTimeMillis();
channelCopy1(src, des);
long end_time5 = System.currentTimeMillis();
System.out.println("普通复制花费的时间是" + (end_time1 - start_time1) + "ms");
System.out.println("使用缓冲流复制花费的时间是" + (end_time2 - start_time2) + "ms");
System.out.println("普通复制使用缓冲数组花费的时间是" + (end_time3 - start_time3) + "ms");
System.out.println("使用缓冲流使用缓冲数组复制花费的时间是" + (end_time4 - start_time4) + "ms");
System.out.println("使用文件通道用复制花费的时间是" + (end_time5 - start_time5) + "ms");
}
}
运行结果