Anyone who is dedicated to the development of applications and web pages has a basic knowledge of Java, perhaps the most popular programming language in the world. The definition given on its official website is as follows: “Java is a programming language and computing platform that was first released by Sun Microsystems in 1995. It has evolved from humble beginnings to power much of today's digital world, providing a reliable platform on which to build. they build many services and applications.”
Being a multiplatform programming language, Java has been given several uses, among these the following stand out:
Learn about the benefits of hiring a JavaScript developer for a banking project. Let's talk.
In order to clean the screen in Java, there are three methods that are the most used by expert developers in this language:
This ANSI escape sequence can be defined as standard in-band signaling to control cursor position. In this tutorial, the escape code \033[H\033[2J will be used. Explained separately:
\033: Represents the ASCII escape character. Its ANSI value is 27. It means ESC. [: Represents the escape sequence.
Combining the above codes, we get \033[ or ESC[.
public class ClearScreenExample1 { public static void main(String[] args) { System.out.print("\033[H\033[2J"); System.out.flush(); } }
Another popular method to clean the console in Java is the command offered by the platform we are using. To do this, you first get the system property using getProperty() of the System class. Then the command used on the platform to clear the console is selected.
System.getProperty() method
This method is used to get the system property indicated by the specified key. In syntax it looks like this:
public static String getProperty(String key)
Now let's create a program to clean up the console in Java using the platform-specific command:
public class ClearScreenExample2 { public final static void clearConsole() { public static void main(String[] args) { try { final String os = System.getProperty("os.name"); if (os.contains("Windows")) { Runtime.getRuntime().exec("cls"); } } catch (final Exception e) { e.printStackTrace(); } }
More about features that the best Java development company must have. Let's work together.
With this method, the command line interpreter, CMD for its acronym in English, is invoked. After doing this, the interpreter executes the cls command. It can be done using the heritageIO() method.
import java.io.IOException; public class ClearScreenExample3 { public static void main(String... arg) throws IOException, InterruptedException { new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor(); } }
Thus, with these three simple methods, the developer will be able to clean the console in Java.
We recommend you on video