In Java, console.flush() pushes any text still waiting in the console's output buffer out to the terminal immediately. It is the method required by the java.io.Flushable interface, which Console implements.
Output is buffered so that many small writes become one fast write. Usually you never notice, because format(), printf(), and writer().println() flush for you. You need flush() after writer().print(...) or writer().write(...), for example for a prompt with no line break or a progress bar printed piece by piece.
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.flush(...).
Syntax
No arguments and no return value:
Java
Console console = System.console();
console.writer().print("Enter city: "); // no line break, may stay buffered
console.flush(); // now the prompt is visible
String city = console.readLine();
Call
What it does
console.flush()
Writes out any buffered console output now.
console.writer().flush()
Flushes the same output through the PrintWriter.
console.printf(...).flush()
Allowed because printf returns the Console, although printf already flushes.
Flushable f = console;
A Console can be used wherever a Flushable is expected.
Available since Java 6. Console.flush() declares no checked exception, but Flushable.flush() declares IOException, so code that calls it through a Flushable variable must handle or declare IOException.
Prints its own prompt, already flushed, before reading.
Java Examples
Example 1 — Make a print() Prompt Visible Before Reading
A prompt printed with writer().print() has no line break. Flush it so the user sees the question before the program waits.
Java
import java.io.Console;
import java.io.PrintWriter;
public class ConsoleFlushPrompt {
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("Enter your city: ");
console.flush();
String city = console.readLine();
if (city == null) {
out.println("No city entered.");
return;
}
out.println("Fetching the forecast for " + city.trim() + "...");
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleFlushPrompt. (user types ...) marks keyboard input, not program output. The last part is a run where System.console() is null (IDE, redirected input).
Enter your city: Paris (user types: Paris)
Fetching the forecast for Paris...
--- When System.console() returns null ---
No console available. Run this program from a terminal.
How It Works
Prompt without a break.out.print("Enter your city: ") leaves the cursor after the colon, which looks nice but is not flushed automatically.
Flush, then wait.console.flush() sends the prompt to the screen before readLine() blocks; without it the user could stare at an empty line.
Read the answer. The user types Paris on the same line as the prompt and presses Enter, and readLine() returns "Paris".
Reply with println. The final println flushes by itself, so no second flush() is required.
Example 2 — Show a Progress Bar During a Long Loop
Count prime numbers up to 50000 and print one # every 10000 numbers, flushing each time so the bar grows while the work runs.
Java
import java.io.Console;
import java.io.PrintWriter;
public class ConsoleFlushProgress {
static boolean isPrime(int n) {
if (n < 2) {
return false;
}
for (int d = 2; d * d <= n; d++) {
if (n % d == 0) {
return false;
}
}
return true;
}
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("Checking 1 to 50000 ");
int primes = 0;
for (int n = 1; n <= 50000; n++) {
if (isPrime(n)) {
primes++;
}
if (n % 10000 == 0) {
out.print('#');
console.flush();
}
}
out.println();
console.printf("Found %d primes.%n", primes);
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleFlushProgress.
Checking 1 to 50000 #####
Found 5133 primes.
How It Works
Trial division.isPrime tests divisors only up to the square root (d * d <= n), which is enough to find a factor if one exists.
Five checkpoints.n % 10000 == 0 is true at 10000, 20000, 30000, 40000, and 50000, so exactly five # marks are printed.
Flush each mark. Calling console.flush() right after each print('#') makes the bar grow on screen during the loop instead of appearing all at once at the end.
Finish the line.println() ends the bar line, then printf reports that there are 5133 primes from 1 to 50000.
Example 3 — Flush the Console Through the Flushable Interface
Console implements Flushable, so it can go into the same helper as other flushable objects such as a StringWriter.
Java
import java.io.Console;
import java.io.Flushable;
import java.io.IOException;
import java.io.StringWriter;
public class ConsoleFlushFlushable {
static void flushAll(Flushable... targets) throws IOException {
for (Flushable target : targets) {
target.flush();
}
}
public static void main(String[] args) throws IOException {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run this program from a terminal.");
return;
}
StringWriter log = new StringWriter();
log.write("saved 3 records");
console.writer().print("Status: " + log + System.lineSeparator());
flushAll(console, log);
console.printf("Console is a Flushable: %b%n", console instanceof Flushable);
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleFlushFlushable.
Status: saved 3 records
Console is a Flushable: true
How It Works
Interface parameter.flushAll accepts any number of Flushable objects; because Flushable.flush() declares IOException, both it and main declare throws IOException.
Buffered status line. The status text, including a line separator, is written with print, which does not trigger the writer's automatic flush.
Mixed targets.flushAll(console, log) flushes the console (the status line appears) and the StringWriter (which has nothing to do) through the same interface call.
Type check.console instanceof Flushable prints true, confirming the interface relationship used above.
Frequently Asked Questions
It writes out any console output that is still buffered, so it appears on the terminal immediately.
No. format() and printf() flush automatically. flush() matters after writer().print() or writer().write().
If you printed it with console.writer().print(...), it may still be in the buffer. Call console.flush() first, or pass the prompt to console.readLine("Prompt: ") instead.
No. Console.flush() declares no checked exception. Only calls made through a variable of type Flushable have to deal with IOException.
No. They flush different objects. Text written with console.writer() is not flushed by System.out.flush(), and the other way round.