暂停通道到通道复制文件

时间:2013-12-08 21:11:20

标签: java file copy

我的程序是一个文件管理器,当我点击暂停按钮时,我想暂停一个频道来复制频道。 我的复制方法

    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。你能救我吗?

1 个答案:

答案 0 :(得分:1)

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

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

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();
        }
    }
}