Java.io.PrintWriter.flush() Method



Description

The java.io.PrintWriter.flush() method flushes the stream.

Declaration

Following is the declaration for java.io.PrintWriter.flush() method.

public void flush()

Parameters

NA

Return Value

This method does not return a value.

Exception

NA

Example

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

package com.tutorialspoint;

import java.io.*;

public class PrintWriterDemo {
   public static void main(String[] args) {
      String s = "Hello World";
      
      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();
      }
   }
}

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

Hello World
java_io_printwriter.htm
Advertisements