gpt4 book ai didi

java - 在 Java 中连接两个大文件(超过 1.5GB)的最有效(最快)的方法是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:48:35 25 4
gpt4 key购买 nike

我使用了此处的技术并在 70 秒内连接了两个 1.5GB 的文件。

http://nadeausoftware.com/articles/2008/02/java_tip_how_read_files_quickly

我的代码涉及使用具有内存映射的 FileChannels 和具有 8KB 缓冲区大小的 ByteBuffers。

我怎样才能提高这个速度?

File file = new File(binDirectory + "/donjon.avi");
File oFile = new File(binDirectory + "/donjon2.avi");

FileInputStream is = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(oFile);
FileChannel f1 = is.getChannel();
FileChannel f2 = fos.getChannel();

f2.transferFrom(f1, 0, f1.size());
f2.transferFrom(f1, f1.size(), f1.size());

f2.close();
f1.close();

最佳答案

试试这个

    FileChannel c1 = new FileInputStream("1").getChannel();
FileChannel c2 = new FileOutputStream("2", true).getChannel();
c2.transferFrom(c1, c2.size(), c1.size());

javadoc 说 FileChannel.transferFrom 可能比从该 channel 读取并写入目标 channel 的简单循环更有效。许多操作系统可以将字节直接从文件系统缓存传输到目标 channel ,而无需实际复制它们。

关于java - 在 Java 中连接两个大文件(超过 1.5GB)的最有效(最快)的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20897464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com