Does this code work to clear the console screen in java

does this code work to clear the console screen in java

System.out.print("\033[H\033[2J");
      System.out.flush();

Having just tested it in a blank Java repl, yes it does.

1 Like

yes, I can explain. Basically Ansi codes are things you can print to the console that get intrepted. For example, if you have \n it gets interpreted as a newline. In linux, those codes are interpreted as so:

  • \033 is the start is ansi codes
  • go to the beginning of the screen ([H allows you to move the cursor to some x, y coordinate with it moving to the beginning as default – otherwise you can do 033[1;1H)
  • clear everything from this point on the screen onwards with \033[2J

hope this helped

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.