Java Console Methods

About This Index
This page is the Java Console Methods index: every public method of the java.io.Console class, each with a full guide covering syntax, parameters, return value, exceptions, three runnable examples, and FAQs. Each example shows the output of a real terminal run, with the text you type marked clearly.
Learn what the Console class is and why System.console() can be null on the Console hub. Use this page when you need a specific method such as readLine(), readPassword(), or printf(). All examples compile on Java 8 and later, except charset(), which needs Java 17.
Many IDE run windows are not real terminals, so System.console() returns null there. Compile and run with javac Name.java and java Name in Command Prompt, PowerShell, or a macOS/Linux terminal.
How to Call a Console Method
Every method here is an instance method: get the console once, check it for null, then call methods on it:
import java.io.Console;
Console console = System.console();
if (console == null) {
System.out.println("No console available.");
return;
}
String name = console.readLine("Name: "); // read a line
console.printf("Hello, %s!%n", name); // formatted output
char[] pin = console.readPassword("PIN: "); // hidden input Java Console Methods Table
All method guides in this Java tutorial series (8 tutorials), grouped by purpose.
Write output to the terminal
4 methodsPrint formatted text, get the console PrintWriter, and make sure buffered text actually reaches the screen.
| Method | Description | Tutorial |
|---|---|---|
format() | Write formatted text, such as padded columns and rounded numbers, to the terminal. | Try it |
printf() | Print formatted text to the terminal; a convenience alias of format(). | Try it |
writer() | Get the console PrintWriter for print(), println(), and printf() style output. | Try it |
flush() | Force any buffered console output to appear on the screen right away. | Try it |
Read input from the keyboard
3 methodsRead a line of text, read a password without showing it, or get a Reader to wrap in a Scanner or BufferedReader.
| Method | Description | Tutorial |
|---|---|---|
readLine() | Read one line of text typed by the user, optionally after a formatted prompt. | Try it |
readPassword() | Read a password or PIN without showing the typed characters, returned as a char array. | Try it |
reader() | Get the console Reader to wrap in a Scanner or BufferedReader. | Try it |
Console information
1 methodFind out which character encoding the terminal uses (Java 17+).
Frequently Asked Questions
Start with readLine()
Ask the user a question and read the answer, with a prompt and a null check.
11 people found this page helpful
