Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

java - Override previous Console Output

is it possible to override the last System.out.println output, so i can for example visualize changes in a array or create a progessbar?

For example if i have this class:

class Main{
  public static void main(String[] args){
    for(int i = 0; i < 10; i++){
      for(int j = 0; j < i; j++){
        System.out.print("#");
      }
      System.out.println("");
    }
  }
}

What do i have to do to create this simple progressbar which is shown in a single line and not in 10 seperate lines?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This works on my particular console (Windows) but it's not terribly portable...

public class Test {
    public static void main(String[] args) throws Exception {
        for (int i = 0; i < 100; i++) {
            System.out.print("#");
            if (i % 20 == 0) {
                System.out.print("
                    
");
            }
            System.out.flush();
            Thread.sleep(100);
        }
    }
}

There's also the Console class, but that doesn't actually buy you very much as far as I can see...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...