Reputation: 95
I have simple file visitor class which scans all drives within PC to find specific extensions like .txt
. I want to display the progress of this operation with a JProgressBar
. With each scan, the number of total matched files may change. This prevents me from calculating the percentage of the process to display on JProgressBar
.
My code is based on this: Recursive Pattern Matching
How can I calculate and display the percentage when the total number of files is constantly changing?
Upvotes: 0
Views: 263
Reputation: 205785
Using this example, execute your host operating system's find command in the background and display the results in the GUI. Because the number of files is unknown in advance, the program invokes setIndeterminate(true)
on the progress bar. Here are some typical command lines:
ProcessBuilder pb = new ProcessBuilder("find", ".", "-name", "*.txt");
ProcessBuilder pb = new ProcessBuilder("dir", "/s", "*.txt");
Upvotes: 2
Reputation: 1804
The problem is, that you do not know in advance how many files you have. Generelly, you have the option to run through the file system twice: the first time, you just enter directories and only count all files.
Based on this number, you can then calculate a percentage on the second run, where you have a look at the files itself.
This obviously enlongates the process and again you do not have a progress on the first scan.
What you can do, is starting the first scan in a different thread. Assuming your pattern matching needs more time than just counting files, the counting will be faster and you can adjust the JProgressBar dynamically. It will then start unreliable and become more accurate over time.
I guess this aproach is similar to the one implemented in the windows copy mechanism. They also scan files first without progress information and then start copying with progress information. However, this may lead to effects you probably know from windows-machines where file operations have information like "2 hours left" and suddenly after 2 minutes it's done, or vice versa.
Edit:
Sorry, I just right now noticed that you want to iterate through all files on a hard drive and not just starting in some point.
You may sum the file size that you have seen already, if you know the free and total disk space, you can compute the percentage that is already done. However, the progress will most likely not be smooth because file sizes tend to be very different.
Upvotes: 3