0

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();
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
loadP
  • 404
  • 1
  • 4
  • 15
  • Are you trying to say that you would like to print the percentage of bytes read from the input file? – Mifeet May 14 '16 at 20:05
  • yes, I tried to say it. – loadP May 14 '16 at 20:07
  • 1
    Check out [this answer](http://stackoverflow.com/a/240431/2032064). All you need to add is (1) getting the total file size using `File#length()`, (2) add percentage printing to the `read()` method. – Mifeet May 14 '16 at 20:10
  • I understand it in part. May you give a code example ? – loadP May 14 '16 at 20:20

0 Answers0