How to clear the console in Java
In this tutorial, we will look at two ways to clear the console screen in Java. We will learn how to execute Java clear screen commands at runtime through examples.
Clearing the console using ANSI escape codes in Java
We can use special codes called ANSI escape sequences to change the cursor position or display different colors. These sequences can be interpreted as commands composed of a combination of bytes and characters.
To clear the console in Java, we will use escape codes \033[H\033[2J
. This strange set of characters represents the command to clear the console. To understand it better, we can break it down.
The first four characters \033
, , mean , ESC
or the escape character. Combined with 033
and [H
, we can move the cursor to a specific location. The last character, 033[2J
, clears the entire screen.
We can look at the following example, which uses these escape codes. We also use System.out.flush()
, when using System.out.print()
, it is specifically used to clear the remaining bytes so that nothing is missed on the console screen.
For example.
public class ClearConsoleScreen {
public static void main(String[] args) {
System.out.print("Everything on the console will cleared");
System.out.print("\033[H\033[2J");
System.out.flush();
}
}
ProcessBuilder
Clear the console
in Java using
In this method, we will use a ProcessBuilder
, which is a class mainly used to start the process. We can establish a process with a command to clean up the console.
ProcessBuilder()
Receives the command to be executed and its parameters. The problem with this approach is that different operating systems can have different commands to clear the console screen. That's why in our example we check the current operating system.
Finally, we Process
start a new process using the class and inheritIO
set the standard input and output channels to Java I/O channels.
public class ClearScreen {
public static void main(String[] args) {
System.out.println("Hello World");
ClearConsole();
}
public static void ClearConsole() {
try {
String operatingSystem = System.getProperty("os.name") // Check the current operating system
if (operatingSystem.contains("Windows")) {
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "cls");
Process startProcess = pb.inheritIO.start();
startProcess.waitFor();
}
else {
ProcessBuilder pb = new ProcessBuilder("clear");
Process startProcess = pb.inheritIO.start();
startProcess.waitFor();
}
} catch (Exception e) {
System.out.println(e);
}
}
}
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Article URL:https://www.jiyik.com/en/xwzj/prolan_10718.html
Related Articles
console.log in Java
Publish Date:2025/04/14 Views:195 Category:Java
-
This tutorial explains the function in Java console.log() and how to display logs to the console in Java. console.log() is a function in JavaScript that is used to display log messages to the browser console. There is no such message in Jav
Java Change Date Format
Publish Date:2025/04/14 Views:65 Category:Java
-
There are various options available for converting date string to date format. The methods mentioned below can bring the desired result. Let us understand the various ways from the following code block. import java.text.ParseException ; imp
Calendar date in YYYY-MM-DD format in Java
Publish Date:2025/04/14 Views:72 Category:Java
-
Java Date encapsulates the current time and date. The Date class does this with the help of two constructors - Date() and Date(long millisec) constructor. We use Date() the constructor to initialize the object with the current time and date
Implementing a min-max heap in Java
Publish Date:2025/04/14 Views:53 Category:Java
-
In this article, we will implement a max heap and a min heap using PriorityQueue the class. We will also demonstrate inserting and removing elements from the heap. Introduction to Min-Max Heap in Java Heap is a tree-based data structure, wh
Implementing a Min Heap in Java
Publish Date:2025/04/14 Views:197 Category:Java
-
A min heap is a heap in which every internal node is less than or equal to the value of its child nodes. We will see in the following points how to implement a min heap with and without using a library. Minimal heap implementation in Java w
Increasing the heap space in Java
Publish Date:2025/04/14 Views:190 Category:Java
-
In Java, the heap space is mainly used for garbage collection and allocating memory for objects. A default heap space is allocated when JVM is installed on our machine, but it may be different. The following points show how we can increase
Detecting EOF in Java
Publish Date:2025/04/14 Views:91 Category:Java
-
In this tutorial, we will see how to while detect EOF( End OF File ) in Java using a loop. We will also discuss developing a program that continues reading content until it reaches the end of a file. From a programming perspective, EOF is a
Get resource URL and content in Java
Publish Date:2025/04/14 Views:97 Category:Java
-
getResource() This tutorial will demonstrate how to use the function to get the resource URL and read the resource file in Java . getResource() Use the function to get the resource URL in Java We will use the method in Java getResource() to
Getting the host name in Java
Publish Date:2025/04/14 Views:78 Category:Java
-
In this tutorial, we will see how to get the IP address and host name using Java API. InetAddress Get the host name in Java using The package java.net contains classes for handling the IP address and host name of the current machine InetAdd