The Console class is used to provide methods to read from and write to the console objects. It extends the Object class. It provides the readPassword() method to read a password from the console. If the underlying virtual machine has a console, it can be obtained by invoking the System.console() method. If no console device is available, then an invocation of the System.console() method returns null.
The Console class provides the following methods:
| Method Name | Description |
|---|---|
| Console format(String str, Object… args) | It writes a formatted string to this console’s output stream using the specified format string and arguments. |
| void flush() | It flushes the console and forces any buffered output to be written immediately. |
| PrintWriter writer() | It retrieves the unique PrintWriter object associated with this console. |
| Console printf(String format, Object… args) | It writes a formatted string to this console’s output stream using the specified format string and arguments. |
| Reader reader() | It retrieves the unique Reader object associated with this console. |
| String readLine() | It reads a single line of text from the console. |
| String readLine(String str, Object… args) | It provides a formatted prompt, then reads a single line of text from the console. |
| char[] readPassword() | It reads a password or passphrase from the console with echoing disabled. |
| char[] readPassword(String str, Object… args) | It provides a formatted prompt, then reads a password or passphrase from the console with echoing disabled. |
Responses to “Which class is used to provide methods for reading and writing console objects?”