Java IO Tutorial - Java Console.flush()








Syntax

Console.flush() has the following syntax.

public void flush()

Example

In the following code shows how to use Console.flush() method.

/*ww  w  . j  ava2  s  . c o  m*/
import java.io.Console;

public class Main {
  public static void main(String[] args) throws Exception {

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

  }
}

The code above generates the following result.