2

Have searched this quite a bit and haven't found anything.

So, we know to print text it is System.out.print or println or whatever your needs are. You can also use System.console() and call methods on that.

However, I see applications which change output which they have added to the screen. For example, removing things which are no longer relevant after they are finished. How is this done in Java?

OllieStanley
  • 712
  • 7
  • 25

1 Answers1

4

You can delete text by using the backspace character \b. For example:

System.out.print("\b");

An example:

public class Test {

    public static final String[] frames = {"|", "/", "-", "\\"};

    public static void main(String[] args) throws Exception {
        System.out.println();

        for (int ctr = 0; ctr < 50; ctr++) {
            System.out.print(frames[ctr % frames.length]);
            Thread.sleep(250);
            System.out.print("\b");
        }
    }
}

Note, however, that this may not work in the Eclipse console, but will work outside of Eclipse.

martinez314
  • 12,162
  • 5
  • 36
  • 63
  • Good answer, thanks for that. I wasn't looking forward to clearing the console and tracking the things I wanted to keep to reprint them. – OllieStanley Sep 05 '14 at 20:36
  • \b will erase only last character ... if you want to erase something in the middle then it won't be useful ... – StackFlowed Sep 05 '14 at 20:43
  • \r also works on most consoles - takes your cursor to start of line –  Sep 05 '14 at 20:46
  • @Aeshang it's easier to clear a single line and then reprint what you want using \b that to clear the entire console and reprint a load of stuff though – OllieStanley Sep 06 '14 at 13:13