Java IO Tutorial - Java Writer.flush()








Syntax

Writer.flush() has the following syntax.

public abstract void flush()   throws IOException

Example

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

/*from ww  w  .  j  a  v a  2s  .  c o  m*/
import java.io.*;

public class Main {

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

      
      Writer writer = new PrintWriter(System.out);

      try {
         // append a string
         writer.append(s);

         // flush the writer
         writer.flush();

         // append a new string in a new line
         writer.append("\nThis is an example");

         // flush the stream again
         writer.close();

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

The code above generates the following result.