package com.yonge.nio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* 檔案傳輸幾種方式比較
* @author wb-gaoy
* @version $Id: ChannelTest.java,v 0.1 2012-12-13 下午7:28:40 wb-gaoy Exp $
*/
public class ChannelTest {
public boolean copyFileByIO(File srcFile, File targetDir) throws IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(targetDir + File.separator + srcFile.getName());
byte[] bytes = new byte[1024 * 1024];
int length = -1;
while ((length = fis.read(bytes)) != -1) {
fos.write(bytes, 0, length);
}
fis.close();
fos.close();
return true;
}
public boolean copyFileByNIO(File srcFile, File targetDir) throws IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(targetDir + File.separator + srcFile.getName());
try {
FileChannel fisChannel = fis.getChannel();
FileChannel fosChannel = fos.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024);
while (fisChannel.read(byteBuffer) != -1) {
byteBuffer.flip();
fosChannel.write(byteBuffer);
byteBuffer.clear();
}
fosChannel.close();
fisChannel.close();
} finally {
fis.close();
fos.close();
}
return true;
}
public boolean copyFileByTransfer(File srcFile, File targetDir) throws IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(targetDir + File.separator + srcFile.getName());
try {
FileChannel fisChannel = fis.getChannel();
FileChannel fosChannel = fos.getChannel();
fisChannel.transferTo(0, fisChannel.size(), fosChannel);
fosChannel.close();
fisChannel.close();
} finally {
fis.close();
fos.close();
}
return true;
}
//注意:如果目标檔案已經存在或磁盤間的格式不一樣(例如源檔案磁盤是ntfs,而目标磁盤是fat32),則不會成功
public boolean moveFile(File srcFile, File targetFile) {
srcFile.renameTo(new File(targetFile + File.separator + srcFile.getName()));
return true;
}
/**
* @param args
*/
public static void main(String[] args) {
final ChannelTest test = new ChannelTest();
final File targetDir = new File("e:/temp");
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
long start = System.currentTimeMillis();
test.copyFileByIO(new File("e:/ppl20120921145445000611.rar"), targetDir);
System.out.println("IO cost:" + (System.currentTimeMillis() - start) + "ms");
} catch (IOException e) {
e.printStackTrace();
}
}
}, "A");
thread1.start();
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
try {
long start = System.currentTimeMillis();
test.copyFileByNIO(new File("e:/ppl20120921145445000612.rar"), targetDir);
System.out.println("NIO cost:" + (System.currentTimeMillis() - start) + "ms");
} catch (IOException e) {
e.printStackTrace();
}
}
}, "B");
thread2.start();
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
long start = System.currentTimeMillis();
test.moveFile(new File("e:/ppl20120921145445000613.rar"), targetDir);
System.out.println("Move cost:" + (System.currentTimeMillis() - start) + "ms");
}
}, "C");
thread3.start();
Thread thread4 = new Thread(new Runnable() {
@Override
public void run() {
long start = System.currentTimeMillis();
try {
test.copyFileByTransfer(new File("e:/ppl20120921145445000614.rar"), targetDir);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Transfer cost:" + (System.currentTimeMillis() - start) + "ms");
}
}, "D");
thread4.start();
}
}
上面是四種檔案傳輸方式,測試結果是transfer的效率最低,而網上都說這種效率高,不知道是什麼原因,歡迎大家測試一下,說說各自的想法。