测试文件大小:5G
//耗时:147801139000
copyUseBIO(copyFile, file);
//耗时:160264982300
nioCopy(copyFile, file);
//耗时:155988801199
nio_mapCopy(copyFile, file);
//耗时:133744337900
nio_transCopy(copyFile, file);
private static void nio_transCopy(String copyFile, File file) throws IOException {
File nioFile = new File(copyFile + "nio_trans_" + file.getName());
if (!nioFile.exists()){
nioFile.createNewFile();
}
FileInputStream fileInputStream = new FileInputStream(file);
FileChannel fileChannel = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream(nioFile);
FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();
long start = System.nanoTime();
Long limit =Long.valueOf(Integer.MAX_VALUE);
long nextPosition = 0;
while (true){
fileChannel.transferTo(nextPosition,limit,fileOutputStreamChannel);
nextPosition = nextPosition + limit + 1L;
if (nextPosition < fileChannel.size()){
if (nextPosition + limit > fileChannel.size()){
limit = fileChannel.size() - nextPosition;
}
}
if (nextPosition > fileChannel.size()){
break;
}
}
System.out.println("耗时:" + (System.nanoTime() - start));
fileOutputStreamChannel.close();
fileOutputStream.close();
fileChannel.close();
fileInputStream.close();
}
private static void nio_mapCopy(String copyFile, File file) throws IOException {
File nioFile = new File(copyFile + "nio_map_" + file.getName());
if (!nioFile.exists()){
nioFile.createNewFile();
}
FileInputStream fileInputStream = new FileInputStream(file);
FileChannel fileChannel = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream(nioFile);
FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();
int i = 0;
long start = System.nanoTime();
Long limit =Long.valueOf(Integer.MAX_VALUE);
long nextPosition = 0;
while (true){
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, nextPosition, limit);
fileOutputStreamChannel.write(mappedByteBuffer);
i++;
nextPosition = nextPosition + limit + 1L;
if (nextPosition < fileChannel.size()){
if (nextPosition + limit > fileChannel.size()){
limit = fileChannel.size() - nextPosition;
}
}else {
break;
}
}
System.out.println("copy 结束:" + i);
System.out.println("耗时:" + (System.nanoTime() - start));
fileOutputStreamChannel.close();
fileOutputStream.close();
fileChannel.close();
fileInputStream.close();
}
private static void nioCopy(String copyFile, File file) throws IOException {
FileInputStream fileInputStream = new FileInputStream(file);
// 利用BIO copy 文件
File nioFile = new File(copyFile + "nio_" + file.getName());
if (!nioFile.exists()){
nioFile.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(nioFile);
FileChannel fileInputStreamChannel = fileInputStream.getChannel();
FileChannel fileOutputStreamChannel = fileOutputStream.getChannel();
ByteBuffer fileByteBuffer = ByteBuffer.allocateDirect(1024);
long start = System.nanoTime();
while (fileInputStreamChannel.read(fileByteBuffer) != -1){
fileByteBuffer.flip();
fileOutputStreamChannel.write(fileByteBuffer);
fileByteBuffer.compact();
}
fileByteBuffer.flip();
while (fileByteBuffer.hasRemaining()){
fileOutputStreamChannel.write(fileByteBuffer);
}
fileOutputStreamChannel.close();
fileOutputStream.close();
fileInputStreamChannel.close();
fileInputStream.close();
System.out.println("耗时:" + (System.nanoTime() - start));
}
private static void copyUseBIO(String copyFile, File file) throws IOException {
FileInputStream fileInputStream = new FileInputStream(file);
// 利用BIO copy 文件
File bioFile = new File(copyFile + "bio_" + file.getName());
if (!bioFile.exists()){
bioFile.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(bioFile);
byte[] bioBytes = new byte[1024];
long start = System.nanoTime();
while (fileInputStream.read(bioBytes) != -1){
fileOutputStream.write(bioBytes);
}
fileOutputStream.flush();
fileOutputStream.close();
fileInputStream.close();
System.out.println("耗时:" + (System.nanoTime() - start));
}