Java - Console flush() method



Description

The Java Console flush() method is used to ensure that any buffered output is immediately written to the underlying stream. flush() method of Console ensures immediate display of printed text before buffering delays. It's useful for real-time progress messages and interactive input prompts. It prevents unexpected buffering behavior when printing without a newline.

Declaration

Following is the declaration for java.io.Console.flush() method −

public void flush()

Parameters

NA

Return Value

This method does not return any value.

Exception

NA

Important Note

System.console() may return null in IDEs like Eclipse, NetBeans or IntelliJ. Run these programs from the command line (terminal) for proper execution.

Example - Usage of Console flush() method

The following example shows the usage of Java Console flush() method.

ConsoleDemo.java

package com.tutorialspoint;

import java.io.Console;

public class ConsoleDemo {
   public static void main(String[] args) {      
      Console console = null;

      try {
         //Create a console object.
         console = System.console();

         // test for console not null
         if (console != null) {

            // read line from the console
            String name = console.readLine("Enter  name  : ");

            // print
            System.out.println("You have entered : " + name);
         }

         // flushes console and forces output to be written
         console.flush();   

      } catch(Exception ex) {
         // if any error occurs
         ex.printStackTrace();      
      }
   }
}

Console Interaction

Let us compile and run the above program, this will produce the following result −

Master Programmer
You have entered : Master Programmer

Example - Ensuring Immediate Output Before a Delay

The following example shows the usage of Java Console flush() method. This example demonstrates using flush() to display text immediately before executing a time-consuming operation.

ConsoleDemo.java

package com.tutorialspoint;

import java.io.Console;

public class ConsoleDemo {
   public static void main(String[] args) {
      Console console = System.console();

      if (console != null) {
         // Print without newline and flush to ensure it's displayed immediately
         console.writer().print("Processing...");
         console.flush();  // Forces immediate display of "Processing..."

         // Simulating a long-running task
         try {
            Thread.sleep(3000); // Simulate 3 seconds delay
         } catch (InterruptedException e) {
            e.printStackTrace();
         }

         console.writer().println(" Done!"); // Completes the message
		 console.flush();
      } else {
         System.out.println("No console available.");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Processing... Done!

Explanation

  • console.writer().print("Processing...") prints the text without a newline.

  • console.flush() forces the buffer to flush, making "Processing..." visible immediately.

  • A 3-second delay simulates a long-running operation.

  • After completion, " Done!" is printed.

Without flush(), "Processing..." might not appear until the program completes the delay.

Example - Flushing Output Before User Input

The following example shows the usage of Java Console flush() method. This example ensures that a prompt is displayed before waiting for user input.

ConsoleDemo.java

package com.tutorialspoint;

import java.io.Console;

public class ConsoleDemo {
   public static void main(String[] args) {
      Console console = System.console();

      if (console != null) {
         // Print a prompt message without newline
         console.writer().print("Enter your name: ");
         console.flush();  // Ensures the prompt is displayed before waiting for input

         // Read user input
         String name = console.readLine();
         console.writer().println("Hello, " + name + "!");
         console.flush();
      } else {
         System.out.println("No console available.");
      }
   }
}

Console Interaction

Let us compile and run the above program, this will produce the following result −

Enter your name: Sunil
Hello, Sunil!

Explanation

  • console.writer().print("Enter your name: ") prints a prompt without a newline.

  • console.flush() ensures the prompt is displayed before waiting for user input.

  • console.readLine() takes user input.

  • The program then prints "Hello, <name>!".

  • Without flush(), the prompt "Enter your name: " might not appear before the program waits for input.

java_io_console.htm
Advertisements