如何使用FileChannels transferFrom()方法监控进度(JProgressBar)? [英] How to monitor progress (JProgressBar) with FileChannels transferFrom() method?

查看:626
本文介绍了如何使用FileChannels transferFrom()方法监控进度(JProgressBar)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助 JProgressBar 组件。我的程序使用 java.nio FileChannels 将文件从一个地方复制到另一个地方。实际的复制方法是 transferFrom()

I need a little help with the JProgressBar component. My program copies files from one place to another using java.nio FileChannels. The actual copy method is transferFrom().

我现在有两个问题。


  1. 如何监控FileChannel的传输进度?我发现的所有教程都使用传统的java.io InputStreams,并在循环输入流时增加进度int。

  1. How to I monitor the transfer progress of FileChannels? All tutorials I've found use the conventional java.io InputStreams and increase the progress int while looping through the inputstream.

我的复制方法(FileChannel方法)被封装在一个单独的方法中,该方法由其他方法调用,这些方法迭代源文件夹和目标文件夹,然后为每个文件调用FileChannel方法。

My copy methods (the FileChannel methods) are encapsulated in a separate method that is invoked by other methods that iterate though source and destination folders and then invoke the FileChannel methods for each file.

如何为完整的复制机制实现ProgressBar?

How do I implement a ProgressBar for the complete copy mechanism?

我应该早一点阅读FAQ,所以我想我必须编辑我的初始帖子而不是评论答案,对吗?

Well I should have read the FAQ a little earlier, so I guess I have to edit my initial post instead of commenting the answers, right?

好的,这就是我到目前为止所做的。
就像jambjo建议的那样(顺便说一下), transferFrom()方法现在循环播放。
BTW:是否有一个更好的块大小,或者它取决于我的进度条的粒度,就像EJP所说的那样?

Ok this is what I've done so far. Just as jambjo suggested (thanks by the way), the transferFrom() method is now looped. BTW: Is there a preferable chunk size, or does it depend on the granularity of my progress bar just as EJP said?

这是我的代码片段:

while (position < size) {
 position += destination.transferFrom(source, position, chunkSize);
 current =  (position/size)*100;
 System.out.println(current);
}

不幸的是,'当前'值在循环中保持为0并且我有不明白为什么。
我错过了什么吗?

Unfortunately the 'current' value stays 0 within the loop and I've got no idea why. Am I missing something?

再次感谢 jambjo !我非常感谢你的投入!
现在单个文件的进度监控工作,让我们解决第二个问题。

Thanks again jambjo! I really appreciate your input! Now that the progress monitoring of a single file works, let's address my second problem.

我是喜欢,不,我不得不监控一个文件的进度,而不是一堆文件。
我的主要复制方法遍历各种目录并通过调用实际的传输方法复制适当的文件。
因此复制方法不传输文件,它们只是选择实际传输方法的文件。

I'd like, no I have to, monitor progress of not only a single file, but rather a bunch of files. My main copy methods iterates through various directories and copies appropriate files by invoking the actual transfer method. So the copy methods don't transfer files, they are just selecting files for the actual transfer method.

推荐答案

无法监视 transferFrom 的单个调用的进度,但由于您可以传递它的偏移量和长度参数,您可以围绕它实现自己的循环并更新适当大小的数据块之间的进度条。

There is no way to monitor the progress of a single invokation of transferFrom, but since you can pass it offset and length parameters, you can implement your own loop around it and update the progress bar between suitable sized chunks of data.

这篇关于如何使用FileChannels transferFrom()方法监控进度(JProgressBar)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆