这是我的代码:
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%
并且不移动。怎么了?
答案 0 :(得分:1)
我知道这很老,但是多年来我已经多次发现此线程,而不得不解决这个问题。正如@Roberto所指出的,它是Java中的bug。我做了一个类似的解决方法,该错误报告程序发布了该问题。
我希望这对遇到此问题的其他人有所帮助。