-1

Is there a way to clear multiple lines of text printed in the console with Java?

I'm trying to print 2 pictures made with text that are slightly different and replace each other in a loop. Thus, I want to print a multiline string and then clear the screen before printing the next string. However, I don't know how to clear the screen.

I've already tried using \r, but that doesn't work because it's multiple lines.

Gareth
  • 47
  • 1
  • 2
  • 7
  • Sure...it's called ASCII Art but it won't overwrite a previous Ascii image in the Console Window – DevilsHnd - 退職した Jan 15 '21 at 01:14
  • There are some [Java curses-like libraries](https://stackoverflow.com/questions/439799/whats-a-good-java-curses-like-library-for-terminal-applications) that may be able to do what you want. I haven't tried any of them, personally. – David Conrad Jan 15 '21 at 01:28
  • @DevilsHnd I know what ASCII art is but what im trying to do is override what was previously printed. – rinkimekari Jan 15 '21 at 01:48
  • I don't believe you can do that unless you either clear the screen every 500ms so that it appears to be animating, or add enough new lines that the old content scrolls up and is hidden. – sorifiend Jan 15 '21 at 02:05

1 Answers1

2

u/sellithy on reddit helped me out with this link and the block of code that worked for me was

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