Last Updated : 11 Sep, 2023
The Java.io.Console class provides methods to access the character-based console device, if any, associated with the current Java virtual machine. The Console class was added to java.io by JDK 6.
Important Points:static Console console( )If a console is available, then a reference to it is returned. Otherwise, null is returned. A console will not be available in all cases. Thus, if null is returned, no console I/O is possible.
public PrintWriter writer() Returns: The printwriter associated with this console
public Reader reader() Returns: The reader associated with this console
public Console format(String fmt, Object... args) Parameters: fmt - A format string as described in Format string syntax args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. Returns:This console Throws: IllegalFormatException
public Console printf(String format, Object... args) Parameters: format - A format string as described in Format string syntax. args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. Returns:This console Throws:IllegalFormatException
public String readLine(String fmt,Object... args) Parameters: fmt - A format string as described in Format string syntax. args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. Returns: A string containing the line read from the console, not including any line-termination characters, or null if an end of stream has been reached. Throws: IllegalFormatException IOError - If an I/O error occurs.
public String readLine() Returns: A string containing the line read from the console, not including any line-termination characters, or null if an end of stream has been reached. Throws: IOError
public char[] readPassword(String fmt,Object... args) Parameters: fmt - A format string as described in Format string syntax for the prompt text. args - Arguments referenced by the format specifiers in the format string. Returns: A character array containing the password or passphrase read from the console, not including any line-termination characters, or null if an end of stream has been reached. Throws: IllegalFormatException IOError
public char[] readPassword() Returns: A character array containing the password or passphrase read from the console, not including any line-termination characters, or null if an end of stream has been reached. Throws:IOError
public void flush() Specified by: flush in interface Flushable
// Java Program to demonstrate Console Methods
import java.io.*;
class ConsoleDemo
{
public static void main(String args[])
{
String str;
//Obtaining a reference to the console.
Console con = System.console();
// Checking If there is no console available, then exit.
if(con == null)
{
System.out.print("No console available");
return;
}
// Read a string and then display it.
str = con.readLine("Enter your name: ");
con.printf("Here is your name: %s\n", str);
//to read password and then display it
System.out.println("Enter the password: ");
char[] ch=con.readPassword();
//converting char array into string
String pass = String.valueOf(ch);
System.out.println("Password is: " + pass);
}
}
Output:
Enter your name: Nishant Sharma Here is your name: Nishant Sharma Enter the password: Password is: dada
Note: System.console() returns null in an online IDE
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4