I'm trying to make a cross platform console progress indicator in Java. Therefore I use the System.out.printf method to print out a percentage:
System.out.printf("\t%2.2f%%\b\b\b\b\b\b", percentage);
and I place this in a for loop. The problem I encounter is that it's not printing anything until the whole for loop is done. This is a program example to show the problem:
public class Test {
public static void main(String[] args) {
for(int i =0; i<5000; i++){
System.out.printf("\b\b\b\b\b\b%2.2f%%", ((float) i/5000f)*100f);
System.out.flush();
}
}
}
I think the problem has something to do with compiler optimisation, but I'm not shure. The strange thing is that System.out.println
does print when the for loop is running.
Edit: I forgot to add it to the problem. But I had allready tried to flush the buffer. This makes no difference. Adding %n
to the end of my printf line works but it starts a newline, I really need it to reuse the current line.
All opposed solutions work. But they only work in real consoles. Not the netbeans or eclipse console.