Java IO Tutorial - Java Console.flush()


Console.flush() has the following syntax.

public void flush()


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

/*ww  w  . j  ava2  s  . c o  m*/

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


The code above generates the following result.