[英]ProgressMonitorInputStream - progress bar doesn't show the real progress
這是我的代碼:
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);
}
}
}
對於較小的文件,它可以正常工作,但是如果我對3GB的文件嘗試,進度條將顯示錯誤的進度。 當達到100%時,復制尚未完成,在剩余時間內,進度條將設置為0%
,並且不會移動。 它出什么問題了?
我知道這很舊,但是多年來我已經多次發現此線程,而不得不修復相同的問題。 正如@Roberto所指出的,這是Java中的一個錯誤 。 我做了一個類似的解決方法,該錯誤報告者對此進行了發布。
我希望這可以幫助遇到此問題的其他人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.