Java IO Tutorial - Java PrintWriter.flush()








Syntax

PrintWriter.flush() has the following syntax.

public void flush()

Example

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

/*from   www  . ja  v  a  2 s .c  om*/

import java.io.*;

public class Main {

   public static void main(String[] args) {
      String s = "tutorial from java2s.com";
      try {

         // create a new stream at specified file
         PrintWriter pw = new PrintWriter(System.out);

         // write the string in the file
         pw.write(s);

         // flush the writer
         pw.flush();

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

The code above generates the following result.