Java IO Tutorial - Java PrintWriter.close()








Syntax

PrintWriter.close() has the following syntax.

public void close()

Example

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

/*www. j a va  2  s.c o  m*/

import java.io.*;

public class Main {

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

         PrintWriter pw = new PrintWriter(System.out);

         // append the sequence
         pw.append(s);

         // flush the writer
         pw.flush();

         // print another string
         pw.println("This is an example");

         // flush the writer again
         pw.flush();

         // close the writer
         pw.close();

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

The code above generates the following result.