文章目录
文件读写方式及其对比
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
/**
* 文件复制
* @author 48450
*
*/
public class Filecopy {
public static void main(String[] args) throws IOException {
String sourcePath="C:\\Users\\48450\\Desktop\\test\\source\\org.rar";
String targetPath="C:\\Users\\48450\\Desktop\\test\\target\\";
long[] time=new long[10];
int average=0;
for (int i = 0; i < time.length; i++) {
long start=System.currentTimeMillis();
String tempTargetPath=targetPath+"org"+i+".rar";
Filecopy.traditionalCopy(sourcePath, tempTargetPath);
// Filecopy.bufferCopy(sourcePath, tempTargetPath);
// Filecopy.nioCopy(sourcePath, tempTargetPath);
// Filecopy.nioCopy2(sourcePath, tempTargetPath);
long end=System.currentTimeMillis();
time[i]=end-start;
average+=time[i];
}
System.out.println(average/10.0);
}
/**
* 传统文件读写+缓存方式复制文件
* @param sourcePath
* @param targetPath
* @throws IOException
*/
public static void traditionalCopy(String sourcePath,String targetPath) throws IOException {
File source=new File(sourcePath);
if (!source.exists()) {
source.createNewFile();
}
File target=new File(targetPath);
if (!target.exists()) {
target.createNewFile();
}
FileInputStream fis=new FileInputStream(source);
FileOutputStream fos=new FileOutputStream(target);
byte[] buf=new byte[128];
int len=0;
while ((len=fis.read(buf))!=-1) {
fos.write(buf,0,len);
}
fis.close();
fos.close();
}
/**
* BUfferIo缓存方式复制文件
* @param sourcePath
* @param targetPath
* @throws IOException
*/
public static void bufferCopy(String sourcePath,String targetPath) throws IOException {
File source=new File(sourcePath);
if (!source.exists()) {
source.createNewFile();
}
File target=new File(targetPath);
if (!target.exists()) {
target.createNewFile();
}
BufferedInputStream fis=new BufferedInputStream(new FileInputStream(source));
BufferedOutputStream fos=new BufferedOutputStream(new FileOutputStream(target));
int len=0;
while ((len=fis.read())!=-1) {
fos.write((byte)len);
}
fis.close();
fos.close();
}
/**
* nio文件通道方式复制文件
* @param sourcePath
* @param targetPath
* @throws IOException
*/
public static void nioCopy(String sourcePath,String targetPath) throws IOException {
File source=new File(sourcePath);
if (!source.exists()) {
source.createNewFile();
}
File target=new File(targetPath);
if (!target.exists()) {
target.createNewFile();
}
FileInputStream fis=new FileInputStream(source);
FileOutputStream fos=new FileOutputStream(target);
FileChannel sourceChannel=fis.getChannel();
FileChannel targetChannel=fos.getChannel();
targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
sourceChannel.close();
targetChannel.close();
fis.close();
fos.close();
}
/**
* 文件内存映射及件通道方式复制文件
* @param sourcePath
* @param targetPath
* @throws IOException
*/
public static void nioCopy2(String sourcePath,String targetPath) throws IOException {
File source=new File(sourcePath);
if (!source.exists()) {
source.createNewFile();
}
File target=new File(targetPath);
if (!target.exists()) {
target.createNewFile();
}
FileInputStream fis=new FileInputStream(source);
FileOutputStream fos=new FileOutputStream(target);
FileChannel sourceChannel=fis.getChannel();
FileChannel targetChannel=fos.getChannel();
MappedByteBuffer mappedByteBuffer=sourceChannel.map(FileChannel.MapMode.READ_ONLY, 0, sourceChannel.size());
targetChannel.write(mappedByteBuffer);
sourceChannel.close();
targetChannel.close();
fis.close();
fos.close();
}
}
测试的文件是156MB的压缩文件,测试复制十遍的平均花费时间。
测试速率对比(单位ms)
传统缓存 BufferIO NIO文件通道 文件内存映射+通道
第一次测试:10290.4 4399.9 3729.0 6837.0
第二次测试:10639.0 4481.0 3875.1 8146.9