GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||
GeezerVison is showing zetcode.com — Java Console - reading and writing to the console in Java Java Console - reading and writing to the consolelast modified July 18, 2026
Java Console tutorial shows how to read input from and write output to the
console using Java Console
Not all Java environments have a console. For instance, if the JVM is
started from a background job scheduler or from within an IDE, the console
may not be available. In such cases Reading input with readLine
The Main.java
void main() {
var console = System.console();
if (console == null) {
System.out.println("No console available");
return;
}
var name = console.readLine("Enter your name: ");
console.printf("Hello, %s!%n", name);
}
We obtain the console via $ java Main.java Enter your name: John Hello, John! Reading passwords with readPassword
The Main.java
void main() {
var console = System.console();
if (console == null) {
System.out.println("No console available");
return;
}
var user = console.readLine("Username: ");
var passwd = console.readPassword("Password: ");
if (passwd != null) {
var input = new String(passwd);
if ("admin".equals(user) && "s3cret".equals(input)) {
console.printf("Welcome, %s!%n", user);
} else {
console.printf("Invalid credentials%n");
}
Arrays.fill(passwd, ' ');
}
}
The password is read as a character array with echoing disabled,
preventing the password from being visible on screen. We convert the
character array to a $ java Main.java Username: admin Password: Welcome, admin! Formatted output with format and printf
The Main.java
void main() {
var console = System.console();
if (console == null) {
System.out.println("No console available");
return;
}
console.format("Name: %s, Age: %d%n", "John Doe", 34)
.format("Salary: $%.2f%n", 45600.50)
.format("Active: %b%n", true);
}
$ java Main.java Name: John Doe, Age: 34 Salary: $45600.50 Active: true Using the writer
The Main.java
void main() {
var console = System.console();
if (console == null) {
System.out.println("No console available");
return;
}
var writer = console.writer();
writer.println("Console output via PrintWriter");
writer.println("Line 2");
writer.printf("PI: %.5f%n", Math.PI);
writer.flush();
}
The $ java Main.java Console output via PrintWriter Line 2 PI: 3.14159 Using the reader with Scanner
The Main.java
void main() {
var console = System.console();
if (console == null) {
System.out.println("No console available");
return;
}
try (var scanner = new Scanner(console.reader())) {
var name = scanner.nextLine();
var age = scanner.nextInt();
var salary = scanner.nextDouble();
console.format("%s is %d years old and earns $%.2f%n",
name, age, salary);
}
}
We create a $ java Main.java Jane Smith 28 52000.00 Jane Smith is 28 years old and earns $52000.00 The charset method
The Main.java
void main() {
var console = System.console();
if (console == null) {
System.out.println("No console available");
return;
}
var charset = console.charset();
console.format("Console charset: %s%n", charset);
console.format("Default charset: %s%n",
java.nio.charset.Charset.defaultCharset());
}
The $ java Main.java Console charset: UTF-8 Default charset: UTF-8 The isTerminal method
The Colorized vs plain outputWhen output is sent to a terminal, we can use ANSI escape codes to add color. When piped to another program, the codes are stripped to keep the output clean. Main.java
void main() {
var console = System.console();
if (console == null) {
System.out.println("No console available");
return;
}
var color = console.isTerminal();
var green = color ? "\u001b[32;1m" : "";
var red = color ? "\u001b[31;1m" : "";
var reset = color ? "\u001b[0m" : "";
console.format("%sOK%s Server is running%n", green, reset);
console.format("%sERR%s Connection refused%n", red, reset);
}
We check
Note that when the output is piped, $ java Main.java OK Server is running ERR Connection refused Interactive confirmation vs silent modeWhen running interactively, the program asks for confirmation before proceeding. When piped, it skips the prompt and uses a default behavior. Main.java
void main() {
var console = System.console();
if (console == null) {
System.out.println("No console available");
return;
}
if (console.isTerminal()) {
var reply = console.readLine("Delete all files? (y/n): ");
if (!"y".equals(reply)) {
console.format("Aborted%n");
return;
}
}
console.format("Deleting files...%n");
}
In an interactive terminal, the user is prompted to confirm the
operation. In a non-interactive setting, $ java Main.java Delete all files? (y/n): n Aborted $ java Main.java Delete all files? (y/n): y Deleting files... Console vs Scanner — when to use which
Both
Use
Use
The two can be combined: wrap SourceJava Console - language reference
In this article we have used Java's AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. | ||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |