我想找出在串联Java中的文本文件时我想出的哪一种方法更好.如果有人可以提供一些见解,并且可以就内核级别发生的事情进行共享,这些事情可以解释这些写入FileChannel的方法之间的区别,那么我将不胜感激.
据我从文档和其他Stack Overflow对话中了解到,allocateDirect在驱动器上分配空间,并且大多数情况下避免使用RAM.我担心如果File infile很大(例如1GB),则用allocateDirect创建的ByteBuffer可能有溢出或未分配的可能.目前,在我们开发软件时,我保证文件将不大于2 GB.但将来可能会达到10GB或20GB.
我观察到transferFrom循环从未经历过一次以上循环……因此似乎可以一次成功写入整个infile;但我尚未使用大于60MB的文件进行测试.我循环了一下,因为文档指定了不能保证一次写入多少内容.使用transferFrom仅能在我的系统上接受int32作为其count参数,我一次不能指定超过2GB的内存…再次,内核专业知识将帮助我理解.
在此先感谢您的帮助!!
使用ByteBuffer:
boolean concatFiles(StringBuffer sb, File infile, File outfile) {
FileChannel inChan = null, outChan = null;
try {
ByteBuffer buff = ByteBuffer.allocateDirect((int)(infile.length() + sb.length()));
//write the stringBuffer so it goes in the output file first:
buff.put(sb.toString().getBytes());
//create the FileChannels:
inChan = new RandomAccessFile(infile, "r" ).getChannel();
outChan = new RandomAccessFile(outfile, "rw").getChannel();
//read the infile in to the buffer:
inChan.read(buff);
// prep the buffer:
buff.flip();
// write the buffer out to the file via the FileChannel:
outChan.write(buff);
inChan.close();
outChan.close();
} catch...etc
}
使用trasferTo(或transferFrom):
boolean concatFiles(StringBuffer sb, File infile, File outfile) {
FileChannel inChan = null, outChan = null;
try {
//write the stringBuffer so it goes in the output file first:
PrintWriter fw = new PrintWriter(outfile);
fw.write(sb.toString());
fw.flush();
fw.close();
// create the channels appropriate for appending:
outChan = new FileOutputStream(outfile, true).getChannel();
inChan = new RandomAccessFile(infile, "r").getChannel();
long startSize = outfile.length();
long inFileSize = infile.length();
long bytesWritten = 0;
//set the position where we should start appending the data:
outChan.position(startSize);
Byte startByte = outChan.position();
while(bytesWritten < length){
bytesWritten += outChan.transferFrom(inChan, startByte, (int) inFileSize);
startByte = bytesWritten + 1;
}
inChan.close();
outChan.close();
} catch ... etc
解决方法:
transferTo()效率更高,因为数据复制较少,如果可以全部在内核中完成,则无效率.而且,如果它不在您的平台上,它仍然会使用经过高度调整的代码.
您确实需要循环,有一天它将循环并且您的代码将继续工作.