Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท I/O and Files

The Console class

Reference for java.io.Console: read lines and hidden passwords from a terminal, check isTerminal, and fall back to Scanner or BufferedReader when no terminal exists.

java.io.Console is a thin wrapper around the interactive terminal. Its main value is readPassword(), which reads a line without echoing the characters and returns a char[] you can wipe afterwards. It also offers readLine(prompt) and printf() for prompt-and-answer programs.

You get the instance from System.console(). In the past that method returned null whenever standard input or output was redirected, so every program had to handle a missing console. Since Java 22 it normally returns an object even when input is redirected, and you ask isTerminal() whether a real terminal is attached. Either way, write code that still works without one.

Java
Console console = System.console();
if (console != null && console.isTerminal()) {
    String name = console.readLine("Name: ");
}

Try it

Java Using the console if there is one

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java A prompt helper that hides the Console API

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public final class Console implements Flushable
public static Console System.console()
Member Description
String readLine() Reads one line, without the terminator; null at end of input.
String readLine(String fmt, Object... args) Prints a formatted prompt, then reads a line.
char[] readPassword() Reads a line with echo disabled.
char[] readPassword(String fmt, Object... args) Same, with a formatted prompt.
Console printf(String fmt, Object... args) Formatted output; returns the console.
Console format(String fmt, Object... args) Same as printf.
Console flush() / void flush() Flushes buffered output.
PrintWriter writer() / Reader reader() Character streams for the terminal.
boolean isTerminal() true if the console is an interactive terminal (Java 22+).
Charset charset() The charset used by the console (Java 17+).

Throws: IOError when reading or writing fails (not a checked IOException). IllegalFormatException for a bad prompt format.

Usage notes

  • Check for null. System.console() returns null when the runtime has no console, for instance in some IDEs, services and containers. Programs for older Java versions also see null whenever input or output is redirected.
  • isTerminal() is Java 22 and newer. It lets you tell a real terminal from a pipe or a file. In an IDE run window it is usually false.
  • Passwords as char[]. A String stays in memory until the garbage collector runs and can be interned; a char[] can be overwritten with Arrays.fill as soon as you have hashed or used it.
  • Prompt formatting. The prompt of readLine(fmt, args) is a format string, so a % in user-provided text must be doubled. readLine("%s", text) is the safe way to show arbitrary text.
  • No Unicode guarantee. The console charset is the terminal's. Check charset() if you print non-ASCII text.
  • Use Scanner or BufferedReader over System.in when you also want to support piped input, tests or automated runs. See BufferedReader and Scanner.
  • Not suitable for passwords in scripts. If there is no terminal, nothing is hidden; real tools fail instead of reading the secret in the clear.