Jump to Real's How-to Main page

Clear the console and control attributes

On Win9x, this can be done with the driver ANSI.SYS is loaded via the CONFIG.SYS file.

For example, on Win9x installation, you need something like this in the CONFIG.SYS :
device=c:\windows\command\ansi.sys
Then you send the appropriate escape codes to the console
public class AnsiConsole {
  public static void main(String args[]) {
    String ANSI_CLS = "\u001b[2J";
    String ANSI_HOME = "\u001b[H";
    String ANSI_BOLD = "\u001b[1m";
    String ANSI_AT55 = "\u001b[10;10H";
    String ANSI_REVERSEON = "\u001b[7m";
    String ANSI_NORMAL = "\u001b[0m";
    String ANSI_WHITEONBLUE = "\u001b[37;44m";

    System.out.println(ANSI_CLS);
    System.out.println
       (ANSI_AT55 + ANSI_REVERSEON + "Hello world" + ANSI_NORMAL);
    System.out.println
       (ANSI_HOME + ANSI_WHITEONBLUE + "Hello world" + ANSI_NORMAL);
    System.out.print
       (ANSI_BOLD + "Press a key..." + ANSI_NORMAL);
    try {System.in.read();}catch(Exception e){}
    System.out.println(ANSI_CLS);
    }
  }
NOTE: Check this "old" text file to have an overview of ANSI.SYS's Escape Sequences.

NT's CMD.EXE does not support ANSI escape sequences. The Good news is that you can use COMMAND.COM instead. To use ANSI.SYS, add the following lines in the CONFIG.NT file:
 dosonly
 device=c:\winnt\system32\ansi.sys
But the bad news is that you are restricted this way to run only DOS-based application so it won't work for Java stuff!!!

So the only solution left for NT (unless you write some JNI functions) is to use brute force.
for (int i=0; i<25; i++)
    System.out.println();


Written and compiled by Réal Gagnon ©1998-2000 mailto:[email protected]

[ home ]