[英]ProgressMonitorInputStream - progress bar doesn't show the real progress
Here's my code: 这是我的代码:
class Copy extends SwingWorker<Void, Void> {
private File selectedfile = new File("D:/Adatok/proba.file");
private File chosenDestination = new File("D:/Adatok/ide/proba.file");
@Override
protected Void doInBackground() throws Exception {
try {
FileInputStream fileInputStream = new FileInputStream(
selectedfile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
ProgressMonitorInputStream progressMonitorInputStream;
progressMonitorInputStream = new ProgressMonitorInputStream(Panel.this,"Copying...", bufferedInputStream);
File outputFile = new File("" + chosenDestination);
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
int data;
byte[] buffer = new byte[1024];
while ((data = progressMonitorInputStream.read(buffer)) > 0) {
bufferedOutputStream.write(buffer);
}
bufferedOutputStream.close();
progressMonitorInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public void done() {
JOptionPane.showMessageDialog(Panel.this, "Ready!", "Done", 1);
}
}
}
It works fine with smaller files, but if I try it with a 3GB file, the progressbar shows wrong progress. 对于较小的文件,它可以正常工作,但是如果我对3GB的文件尝试,进度条将显示错误的进度。 When it's 100% the copying isn't finished, in the remaining time the progress bar is set to
0%
and doesn't move. 当达到100%时,复制尚未完成,在剩余时间内,进度条将设置为
0%
,并且不会移动。 What's wrong with it? 它出什么问题了?
I know that this is old but I have found this thread more than once over the years while having to fix this same problem. 我知道这很旧,但是多年来我已经多次发现此线程,而不得不修复相同的问题。 It is a bug in Java as @Roberto has pointed out.
正如@Roberto所指出的,这是Java中的一个错误 。 I did a similar workaround that the bug reporter posts about.
我做了一个类似的解决方法,该错误报告者对此进行了发布。
I hope this helps someone else who runs across this problem. 我希望这可以帮助遇到此问题的其他人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.