I was trying to make the progress work in time with the program, while it's time goes on.
In the below code the progress is working but just inside the for loop. How to let the progress grow with the task of the program ?
private void readFromFile(File cf2) throws IOException, Exception {
//System.out.println("okay");
FileReader fr = new FileReader(cf2);
try (BufferedReader bw = new BufferedReader(fr)) {
System.out.println("Wait while reading !");
for (int i = 0 ; i <= 100 ; i++) {
Thread.sleep(10);
System.out.print(String.format("[%s]%d%%\r", progress(i), i));
}
while(bw.readLine() != null)
s1 += bw.readLine();
}
}
private static final StringBuilder res = new StringBuilder();
static String progress(int pct) {
res.delete(0, res.length());
int numPounds = (pct + 9) / 10;
for (int i = 0 ; i != numPounds ; i++) {
res.append('#');
}
while (res.length() != 10) {
res.append(' ');
}
return res.toString();
}