Console in Java is a utility class in java.io package that provides access to the system console associated with JVM.
Console class was introduced in Java 1.6 version. It is mainly used for reading data from the console and writing data on the console. All read and write operations are synchronized.
Here, console refers to the character input device (keyboard) and character display device (screen display).
This utility class provides the various methods to access character-based console device connected with JVM (Java Virtual Machine).
Java Console class declaration
Console is a final class that extends Object class and implements Flushable interface. The general syntax to declare console class in java is as follows:
public final class Console extends Object implements Flushable
The inheritance diagram for this console class is as follows:
java.lang.Object java.io.Console
Constructor of Console class
Console class does not provide any constructor in Java. We can create an object reference to the console using System.console() method if JVM is connected to the console.
The syntax to get object reference of console class is as follows:
Console c = System.console(); Here, System is a class that provides a static method console().
If JVM is not connected with any console, this method will return null.
Methods of Console class in Java
In addition to methods inherited from Object class, Java console class also provides some useful methods that are as follows:
1. void flush(): This method flushes the console and forces all buffered output to be written immediately.
2. Console format(String fmtString, Object… args): This method writes a formatted string to this console’s output stream according to the specified format string fmtString and arguments args.
3. Console printf(String format, Object… args): This method writes a formatted string to this console’s output stream using the specified format string and arguments.
4. Reader reader(): It is used to retrieve the Reader object reference associated with this console. That is, this method returns a reference to a Reader connected to the console.
5. String readLine(): The readLine() method is used to read a single line of text from the console. It reads and returns a string entered from the keyboard or user.
Input ends when the user presses ENTER. Null is returned if the end of the console input stream has been reached. On failure, an IOError is thrown.
6. String readLine(String fmtString, Object… args): This method displays a prompting string formatted using specified fmtString and args, and then reads a single line of text from the console.
Input ends when the user presses ENTER. Null is returned if the end of the console input stream has been reached. On failure, an IOError is thrown.
7. char[ ] readPassword(): This method reads a password from the console with echoing.
8. char[ ] readPassword(String fmtString, Object… args): This overloaded method displays a prompting string formatted as specified by fmtString and args, and then reads a password from the console with echoing disabled.
9. PrintWriter writer(): The writer() method is used to retrieve the PrintWriter object reference associated with this console. That is, it returns an object reference to a Writer connected to the console.
Console Example Program
1. Let’s take a simple example program where we will take input from the keyboard or user and display it on the console.
Program code 1:
import java.io.Console; public class ConsoleExample { public static void main(String[] args) { // Create a reference to the console. Console c = System.console(); // Checking the console is available or not. if(c != null) { System.out.printf("Console is available."); } else{ System.out.printf("Console is not available."); return; // A console is not available. } // Read a string and then display it on the console. System.out.println("Enter your name: "); String name = c.readLine(); // Reads a line of text from the console. System.out.println("Welcome to " +name); } }
Output: Enter your name: John Welcome to John
In this program, we have used printf() method provided by Java console class. The printf() method displays formatted string on the console. We can also use printf() method of PrintStream class to write the formatted data.
Note: If you execute this program using Eclipse IDE, the console may not be available. Try to execute this program from the command prompt.
2. Let’s create a program where we will read the password using readPassword() method of the console class. If we read the password using Console class, it will not be visible to the user.
Look at the source code to understand better.
Program code 2:
import java.io.Console; public class ConsoleExample { public static void main(String[] args) { // Create a reference to the console. Console c = System.console(); // Checking the console is available or not. if(c != null) { System.out.printf("Console is available."); } else{ System.out.printf("Console is not available.%n"); return; } System.out.println("Enter your password: "); char[ ] ch = c.readPassword(); // Reading password. String pass = String.valueOf(ch);// Converting an array of char into string System.out.println("Password is: "+pass); } }
Output: Enter your password: Password is: 1234657
As you can observe in the output, it is not visible when we have entered the password because the program receives it in a character array.
Hope that this tutorial has elaborated all the important points related to Console class in Java with example program. I hope that you will have understood this simple chapter.
In the next tutorial, we will discuss file class in Java with example programs. Please, email us if you find anything incorrect in this tutorial.
Thanks for reading!!!
Next ⇒ File class in Java⇐ Prev Next ⇒