【问题标题】:pause channel to channel copy file暂停频道到频道复制文件
【发布时间】:2013-12-25 21:13:21
【问题描述】:

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

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

【问题讨论】:

    标签: java file copy


    【解决方案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();
            }
        }
    }
    

    【讨论】:

    • 我能想象你在说什么,但我不会写代码……你能给我一个例子吗?
    猜你喜欢
    • 1970-01-01
    • 2015-01-25
    • 2018-12-16
    • 2019-02-19
    • 2020-01-23
    • 1970-01-01
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多