在 java 中更快地合并大文件部分
Merge big file parts faster in java
我正在编写 java 休息服务来支持并行上传大文件的各个部分。我将这些部分写在单独的文件中并使用文件通道合并它们。我有一个用 Golang 实现的示例,它的作用相同,但是当它合并各个部分时,它不需要时间。当我使用文件通道或从一个流中读取并写入最终文件时,需要很长时间。我认为不同之处在于,Golang 能够按原样将数据保存在磁盘上,并通过不实际移动数据来合并它们。有什么方法可以让我在 java 中做同样的事情吗?
这是我合并部分的代码,我对所有部分循环此方法:
private void mergeFileUsingChannel(String destinationPath, String sourcePath, long partSize, long offset) throws Exception{
FileChannel outputChannel = null;
FileChannel inputChannel = null;
try{
outputChannel = new FileOutputStream(new File(destinationPath)).getChannel();
outputChannel.position(offset);
inputChannel = new FileInputStream(new File(sourcePath)).getChannel();
inputChannel.transferTo(0, partSize, outputChannel);
}catch(Exception e){
e.printStackTrace();
}
finally{
if(inputChannel != null)
inputChannel.close();
if(outputChannel != null){
outputChannel.close();
}
}
}
FileChannel transferTo
的文档指出:
"Many operating systems can transfer bytes directly from the filesystem cache to the target channel without actually copying them."
所以你写的代码是正确的,你看到的低效率可能与底层文件系统类型有关。
我建议的一个小优化是以 append 模式打开文件。
"Whether the advancement of the position and the writing of the data are done in a single atomic operation is system-dependent"
除此之外,您可能还得想办法解决这个问题。例如,第一步创建一个足够大的连续文件。
编辑: 我还注意到您没有明确关闭 FileOutputStream。最好坚持并关闭它,这样所有的文件描述符都会关闭。
我正在编写 java 休息服务来支持并行上传大文件的各个部分。我将这些部分写在单独的文件中并使用文件通道合并它们。我有一个用 Golang 实现的示例,它的作用相同,但是当它合并各个部分时,它不需要时间。当我使用文件通道或从一个流中读取并写入最终文件时,需要很长时间。我认为不同之处在于,Golang 能够按原样将数据保存在磁盘上,并通过不实际移动数据来合并它们。有什么方法可以让我在 java 中做同样的事情吗? 这是我合并部分的代码,我对所有部分循环此方法:
private void mergeFileUsingChannel(String destinationPath, String sourcePath, long partSize, long offset) throws Exception{
FileChannel outputChannel = null;
FileChannel inputChannel = null;
try{
outputChannel = new FileOutputStream(new File(destinationPath)).getChannel();
outputChannel.position(offset);
inputChannel = new FileInputStream(new File(sourcePath)).getChannel();
inputChannel.transferTo(0, partSize, outputChannel);
}catch(Exception e){
e.printStackTrace();
}
finally{
if(inputChannel != null)
inputChannel.close();
if(outputChannel != null){
outputChannel.close();
}
}
}
FileChannel transferTo
的文档指出:
"Many operating systems can transfer bytes directly from the filesystem cache to the target channel without actually copying them."
所以你写的代码是正确的,你看到的低效率可能与底层文件系统类型有关。
我建议的一个小优化是以 append 模式打开文件。
"Whether the advancement of the position and the writing of the data are done in a single atomic operation is system-dependent"
除此之外,您可能还得想办法解决这个问题。例如,第一步创建一个足够大的连续文件。
编辑: 我还注意到您没有明确关闭 FileOutputStream。最好坚持并关闭它,这样所有的文件描述符都会关闭。