Java.io.Console.flush() Method



Description

The java.io.Console.flush() method flushes the console and forces any output to be written immediately.

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

Example

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

package com.tutorialspoint;

import java.io.Console;

public class ConsoleDemo {
   public static void main(String[] args) {      
      Console cnsl = null;
      
      try {
         //Create a console object.
           cnsl = System.console();
           
           // test for console not null
           if (cnsl != null) {
              
              // read line from the console
               String name = cnsl.readLine("Enter  name  : ");
               
               // print
               System.out.println("You have entered : " + name);
           }
           
           // flushes console and forces output to be written
           cnsl.flush();   
           
      } catch(Exception ex) {
         // if any error occurs
         ex.printStackTrace();      
      }
   }
}

Following is the input −

Master Programmer

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

You have entered : Master Programmer
java_io_console.htm
Advertisements