Some people on the Internet have discussed how Java can clear the console. The content is not ideal. So summarize the content.
Intuitive. Let's take a look at the code first.
Import static java. Lang. system. out;
/**
* Build by netbeans
* @ Author Felix Wu
*/
Public class conslerewritetest {
Public static void main (string [] AGRs ){
Out. Print ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ");
Pause ();
Out. Print ("/R" + "");
Int Total = 100;
For (INT I = 1; I <total; I ++ ){
Pause ();
Out. Print ("/R" + I + "/" + total );
}
}
Private Static void pause (){
Try {
Thread. Sleep (1000l );
} Catch (interruptedexception e ){
// Swallow this exception
}
}
}
Strictly speaking, the console is the output device of the Java Process. It corresponds to the system. Out stream in Java. For this stream, you can only input content to it. How can a Java program communicate with the console?
Method 1: Use the ASCII control character '/R '.
'/R' indicates to press enter and return to the beginning of the line. '/N' indicates line feed, that is, moving to the next line (the cursor moves down vertically, if the cursor is at the nth character position in the original line, so it moves down to the nth character position in the next line ). "/R/N" indicates that the carriage return is a line break, that is, the line that moves to the beginning of the next line.
Note that '/R' and'/N' are interpreted as standard ASCII codes. However, different software may have different interpretations of '/N. For example, according to the ASCII code standard, the carriage return line is "/R/N", while UNIX saves storage space, its API replaces "/R/N" with '/N' when saving text that adopts ASCII encoding ", the '/N' effect is equivalent to "/R/N ". In short, '/N' in UNIX APIs means a carriage return line feed ("/R/N") instead of a standard line feed. JVM also adopts this style. However, in windows, the API adopts the '/N' standard. Different interpretations of '/N' result in different display effects of the same text in different software.
Method 2: communicate with the output device.
I have not tried this method, but it can be implemented using JNI. The output device may be software (such as common console windows) or hardware. If it is software, the Java program communicates with the software to clear the screen (when this function is provided ). If the hardware can communicate with the driver to clear the screen (if this function is provided ).
In the example above, do not run it directly in nb6.8, because the console of nb6.8 does not parse the '/R' control character, which should be a bug. You can run it directly in cmd on the window.