我正在尝试弄清楚使用NIO的FileChannel
和普通的FileInputStream/FileOutputStream
读写文件到文件系统时是否存在性能差异(或优势)。我观察到,在我的机器上,两者的表现水平相同,而且很多时候使用FileChannel
的方式会更慢。请问这两种方法的比较细节。这是我使用的代码,我测试的文件大小约为350MB
。如果我不考虑随机访问或其他高级功能,使用基于NIO的类进行文件I/O是一个好选择吗?
package trialjavaprograms;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class JavaNIOTest {
public static void main(String[] args) throws Exception {
useNormalIO();
useFileChannel();
}
private static void useNormalIO() throws Exception {
File file = new File("/home/developer/test.iso");
File oFile = new File("/home/developer/test2");
long time1 = System.currentTimeMillis();
InputStream is = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(oFile);
byte[] buf = new byte[64 * 1024];
int len = 0;
while((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
fos.close();
is.close();
long time2 = System.currentTimeMillis();
System.out.println("Time taken: "+(time2-time1)+" ms");
}
private static void useFileChannel() throws Exception {
File file = new File("/home/developer/test.iso");
File oFile = new File("/home/developer/test2");
long time1 = System.currentTimeMillis();
FileInputStream is = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(oFile);
FileChannel f = is.getChannel();
FileChannel f2 = fos.getChannel();
ByteBuffer buf = ByteBuffer.allocateDirect(64 * 1024);
long len = 0;
while((len = f.read(buf)) != -1) {
buf.flip();
f2.write(buf);
buf.clear();
}
f2.close();
f.close();
long time2 = System.currentTimeMillis();
System.out.println("Time taken: "+(time2-time1)+" ms");
}
}
transferTo
/transferFrom
更符合在复制文件时的传输惯例。无论哪种方法使用,都不应该让您的硬盘变得更快或更慢,尽管我猜测如果以小块读取并导致读写头花费过多时间寻道,可能会出现问题。 - Tom Hawtin - tackline