In Java, console.writer() returns the java.io.PrintWriter that writes to the terminal. It gives you the familiar print(), println(), printf(), and write() methods, and it returns the same object every time you call it.
Use it when you want to hand console output to code that expects a PrintWriter, or when you simply prefer println(). Text written with print() may wait in a buffer, so follow it with flush() when it must appear right away.
An instance method of the java.io.Console class, available since Java 6. You never create a Console yourself: get it with Console console = System.console(); after import java.io.Console;, and check it for null before calling console.writer(...).
Syntax
Get the writer once and use it like any other PrintWriter:
Java
Console console = System.console();
PrintWriter out = console.writer();
out.println("Hello"); // line break, flushed
out.print("Loading..."); // no line break, may stay buffered
out.flush(); // show it now
out.printf("%d%%%n", 50); // 50%
Call
What it does
console.writer()
Returns the console PrintWriter (the same object each time).
out.println(x)
Prints x followed by a line separator.
out.print(x)
Prints x with no line break; call flush() to show it.
out.checkError()
Flushes, then reports whether an output error has happened.
Available since Java 6. In the JDK, this PrintWriter flushes automatically on println(), printf(), and format(), but not on print() or write(). Calling flush() yourself is harmless and makes the intent clear.
Library declarations
Java
// java.io.Console
public PrintWriter writer()
Parameters
Parameter
Description
None
writer() takes no parameters.
Return Value
Return Value
Description
PrintWriter
The single PrintWriter associated with this console. It encodes text with the console charset.
Example 1 — Build Lines with print() and println()
Mix print() pieces into one line, end it with println(), and flush at the end.
Java
import java.io.Console;
import java.io.PrintWriter;
public class ConsoleWriterBasics {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run this program from a terminal.");
return;
}
PrintWriter out = console.writer();
out.println("Shopping list:");
out.print("- milk");
out.println();
out.print("- eggs (x");
out.print(12);
out.println(")");
out.flush();
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleWriterBasics. The last part is a run where System.console() is null (IDE, redirected input).
Shopping list:
- milk
- eggs (x12)
--- When System.console() returns null ---
No console available. Run this program from a terminal.
How It Works
Grab the writer.console.writer() is stored in out, so the rest of the code reads like ordinary PrintWriter code.
Pieces of one line.print("- milk") writes text without a break; the empty println() then ends the line.
Mixed types.print(12) prints the int as the characters 1 and 2, between two string pieces, before println(")") closes the line.
Final flush. Nothing is left in the buffer here, but the closing flush() guarantees it even if a later edit ends with print().
Example 2 — Send the Same Report to the Console or to a String
A method that takes a PrintWriter can print to the terminal through console.writer() or capture text with a StringWriter.
Java
import java.io.Console;
import java.io.PrintWriter;
import java.io.StringWriter;
public class ConsoleWriterReport {
static void report(PrintWriter out, String title, int[] values) {
int sum = 0;
for (int v : values) {
sum += v;
}
out.printf("%s: %d items, total %d%n", title, values.length, sum);
out.flush();
}
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run this program from a terminal.");
return;
}
int[] sales = {120, 80, 45};
report(console.writer(), "Console", sales);
StringWriter buffer = new StringWriter();
report(new PrintWriter(buffer), "Saved", sales);
console.writer().println("Captured text: " + buffer.toString().trim());
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleWriterReport.
Console: 3 items, total 245
Captured text: Saved: 3 items, total 245
How It Works
One method, any destination.report only knows it has a PrintWriter; it adds 120 + 80 + 45 = 245 and prints one summary line.
Terminal target. Passing console.writer() sends the first summary straight to the screen.
String target. The second call writes into a StringWriter, so nothing appears yet; the text "Saved: 3 items, total 245" waits in memory.
Show the capture.trim() drops the line separator added by %n, and println prints the captured text after a label.
Example 3 — Show Progress Dots with print() and flush()
Partial lines need a flush to appear. The example also confirms that writer() always returns the same object and checks for output errors.
Java
import java.io.Console;
import java.io.PrintWriter;
public class ConsoleWriterDots {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run this program from a terminal.");
return;
}
PrintWriter out = console.writer();
out.print("Saving");
for (int i = 0; i < 3; i++) {
out.print('.');
out.flush();
}
out.println(" done");
console.printf("Same writer every call? %b%n", out == console.writer());
console.printf("Any output error? %b%n", out.checkError());
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleWriterDots.
Saving... done
Same writer every call? true
Any output error? false
How It Works
Start the line.print("Saving") writes the word without a line break, so the dots can follow on the same line.
Dot by dot. Each loop pass prints one '.' character and flushes, so in a slow real task each dot would appear the moment it is printed.
One shared writer. Comparing out with a fresh console.writer() gives true: the console has exactly one writer.
Error flag.checkError() flushes and returns false because every write reached the terminal.
Frequently Asked Questions
The unique PrintWriter connected to the console output. Every call returns the same object.
After println(), printf(), or format() the JDK flushes for you. After print() or write(), call flush() on the writer or on the console before the text must be visible.
Both usually show up in the same terminal, but they are different objects with separate buffers. System.out exists even when there is no console; console.writer() needs System.console() to be non-null.
Yes, that is one of its main uses. The same method can print to the terminal or, given new PrintWriter(new StringWriter()), into a string.
No. Like every PrintWriter it records errors internally instead of throwing. Call checkError() to find out whether something went wrong.