问题
I want a progress bar to indicate files being copied
how to update the progress bar to show the progress of copying a file by combining below lines of code??
here's my code for progressbar.
@FXML
private void startbtnaction(ActionEvent event) {
startbtn.setDisable(true);
progressbar.setProgress(0);
txt.setText("");
cancelbtn.setDisable(false);
copyworker = createWorker(numFiles);
progressbar.progressProperty().unbind();
progressbar.progressProperty().bind(copyworker.progressProperty());
progressindicator.progressProperty().unbind();
progressindicator.progressProperty().bind(copyworker.progressProperty());
copyworker.messageProperty().addListener(new ChangeListener<String>(){
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
txt.setText(newValue+"\n");
}
});
new Thread(copyworker).start();
}
private Task createWorker(int numFiles) {
return new Task(){
@Override
protected Object call() throws Exception {
for (int i = 0; i < numFiles; i++) {
long elapsedtime=System.currentTimeMillis();
copyFile("somefile", "some dest file");
elapsedtime=System.currentTimeMillis()-elapsedtime;
String status=elapsedtime+"milliseconds";
updateMessage(status);
updateProgress(i+1, numFiles);
}
return true;
}
};
}
public void copyFile(String src,String dest)throws InterruptedException{
Random rnd=new Random(System.currentTimeMillis());
long millis=rnd.nextInt(1000);
Thread.sleep(millis);
}
/// ///////////////////////////////////////////////////////////////////////////////
here's my code for file copying..
InputStream istream = RemoteInputStreamClient.wrap(sendappcontent);
BufferedInputStream bis = new BufferedInputStream(istream);
//downloaded file...
File file = new File("D:\\SERVER\\Temp\\" + contentfile.getName());//server saving point
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
FileChannel channel = fileOutputStream.getChannel();
byte b[] = new byte[1024];
long startTime = System.currentTimeMillis();
while (bis.available() > 0) {
bis.read(b);
System.out.println("B");
System.out.println((System.currentTimeMillis() - startTime) / 1000);
ByteBuffer buffer = ByteBuffer.wrap(b);
channel.write(buffer);
}
bis.close();
fileOutputStream.flush();
channel.close();
fileOutputStream.close();
System.out.println("done");
来源:https://stackoverflow.com/questions/25488319/how-to-update-progress-of-the-progress-bar-to-show-the-progress-of-copying-a-fil