Java.io.Console class in Java Last Updated : 11 Sep, 2023 Comments Improve Suggest changes Like Article Like Report 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: It is used to read from and write to the console, if one exists. Console is primarily a convenience class because most of its functionality is available through System.in and System.out. However, its use can simplify some types of console interactions, especially when reading strings from the console. Console supplies no constructors. Instead, a Console object is obtained by calling System.console( ), which is shown here: 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. It provides methods to read text and password. If you read password using Console class, it will not be displayed to the user.The java.io.Console class is attached with system console internally. Important Methods: writer : Retrieves the unique PrintWriter object associated with this console. Syntax: public PrintWriter writer() Returns: The printwriter associated with this console reader : Retrieves the unique Reader object associated with this console. Syntax: public Reader reader() Returns: The reader associated with this console format : Writes a formatted string to this console's output stream using the specified format string and arguments. Syntax: 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 printf : A convenience method to write a formatted string to this console's output stream using the specified format string and arguments. Syntax: 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 readLine : Provides a formatted prompt, then reads a single line of text from the console. Syntax: 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. readLine : Reads a single line of text from the console. Syntax: 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 readPassword: Provides a formatted prompt, then reads a password or passphrase from the console with echoing disabled. Syntax: 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 readPassword : Reads a password or passphrase from the console with echoing disabled Syntax: 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 flush : Flushes the console and forces any buffered output to be written immediately . Syntax: public void flush() Specified by: flush in interface Flushable Program: Java // 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 Comment More info K kartik Improve Article Tags : Java Java-I/O Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like