Java Console writer() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 6+

Definition

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%
CallWhat 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

ParameterDescription
Nonewriter() takes no parameters.

Return Value

Return ValueDescription
PrintWriterThe single PrintWriter associated with this console. It encodes text with the console charset.

Compare with Similar Methods

Other output routes:

MethodHow it differs
flush()Pushes out anything print() left in the buffer.
printf()Console method that formats and flushes in one step.
System.outA PrintStream that exists even when System.console() is null.
reader()The input twin: the console Reader.

Java Examples

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();
    }
}

How It Works

  1. Grab the writer. console.writer() is stored in out, so the rest of the code reads like ordinary PrintWriter code.
  2. Pieces of one line. print("- milk") writes text without a break; the empty println() then ends the line.
  3. Mixed types. print(12) prints the int as the characters 1 and 2, between two string pieces, before println(")") closes the line.
  4. 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());
    }
}

How It Works

  1. One method, any destination. report only knows it has a PrintWriter; it adds 120 + 80 + 45 = 245 and prints one summary line.
  2. Terminal target. Passing console.writer() sends the first summary straight to the screen.
  3. String target. The second call writes into a StringWriter, so nothing appears yet; the text "Saved: 3 items, total 245" waits in memory.
  4. 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());
    }
}

How It Works

  1. Start the line. print("Saving") writes the word without a line break, so the dots can follow on the same line.
  2. 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.
  3. One shared writer. Comparing out with a fresh console.writer() gives true: the console has exactly one writer.
  4. 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.

Back to the method index

See all 8 Console method guides grouped by what they do.

Method index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful