gpt4 book ai didi

java - 暂停 channel 到 channel 复制文件

转载 作者:行者123 更新时间:2023-12-01 13:44:23 36 4
gpt4 key购买 nike

我的程序是一个文件管理器,当我单击暂停按钮时,我想暂停 channel 以 channel 复制文件。我的复制方法

    fcin = new FileInputStream(source.getPath().replace("\\", "/")).getChannel();
fcout = new FileOutputStream(dest.getPath().replace("\\", "/")).getChannel();
processValue value = new processValue(bar, source, dest);
Thread t1 = new Thread(value);
t1.start();
this.setVisible(true);
fcin.transferTo(0, fcin.size(), fcout);
retVal = true;

和 t1 用于此过程的 JProgressBar。你能帮我吗?

最佳答案

将 fcin.size() 参数更改为某个合理的 block 大小,并将 0 更改为某个计数,然后在检查暂停标志大小的循环中调用它,请参阅 this有关 block 传输示例的问题。

如果您可以添加一个切换其状态的暂停按钮:

public static void fileCopy(File in, File out) throws IOException {
FileChannel inChannel = new FileInputStream(in).getChannel();
FileChannel outChannel = new FileOutputStream(out).getChannel();
try {
// inChannel.transferTo(0, inChannel.size(), outChannel); // original -- apparently has trouble copying large files on Windows
// magic number for Windows, (64Mb - 32Kb) DO NOT EXCEED (64 * 1024 * 1024) - (32 * 1024)
int maxCount = (32 * 1024); // This should allow the code to check the pause state
long size = inChannel.size();
long position = 0;
while (position < size) {
if (not PauseButton.GetState() == PausedState) { // <--- This is the bit you will have to add
position += inChannel.transferTo(position, maxCount, outChannel);
}
}
} finally {
if (inChannel != null) {
inChannel.close();
}
if (outChannel != null) {
outChannel.close();
}
}
}

关于java - 暂停 channel 到 channel 复制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20459136/

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