gpt4 book ai didi

以相同速度复制 Java 文件(Linux 服务器上的 USB2 -> USB3)。怎么了?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:34:27 26 4
gpt4 key购买 nike

我有一个非常简单的 Java 文件副本。它将数据库文件复制到我的外部 (usb3) 硬盘大约 6 分钟。

//First database:
try {
fileInputStream = new FileInputStream(selectedfile);
bufferedInputStream = new BufferedInputStream(fileInputStream);
outputFile = new File("" + chosenDestination);
fileOutputStream = new FileOutputStream(outputFile);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
size = selectedfile.length();
byte[] buffer = new byte[1024 * 6];
dbLabel.setText("Copying Database...");
while ((data = bufferedInputStream.read(buffer)) > 0) {
bufferedOutputStream.write(buffer, 0, data);
total += data;
String percent = "" + total / (size / 100);
pbar.setValue(Integer.valueOf(percent));
sizeLabel.setText("(" + total / (1024 * 1024) + " / " + (size / (1024 * 1024)) + " MB) ");
}
} finally {
bufferedOutputStream.close();
fileInputStream.close();
fileOutputStream.close();
bufferedInputStream.close();
}

昨天我买了一张Transcend TS-PDU3 (PCI-E) USB additon card。所以我的电脑和我的硬盘也能连接 USB3。但是当我尝试复制作业时,它以相同的速度复制文件。它是一个 Linux 服务器,所以不需要驱动程序(lspci 看到它),我认为一切正常,所以我认为错误在 java 代码中。我需要为 USB3 选择什么缓冲区大小。就是6*1024是一个很小的buffer size。或者我需要在其他地方搜索错误?谢谢。

最佳答案

如果您使用 Java 7,则可以更轻松地将数据从一个文件复制到另一个文件。使用 NIO,您可以做到这一点:

final Path src = Paths.get(selectedFile);
final Path dst = Paths.get(outputFile);

Files.copy(src, dst);

JVM 在这方面做得很好。如果您没有看到当前代码的改进,那么...首先用这个替换您的代码,然后进一步调查。正如您所说,理论上不需要特定的驱动程序。

关于以相同速度复制 Java 文件(Linux 服务器上的 USB2 -> USB3)。怎么了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21937842/

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