Java.io.PrintWriter.close() Method

Advertisements


Description

The java.io.PrintWriter.close() method closes the stream and releases any system resources associated with it.

Declaration

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

public void close()

Parameters

  • NA

Return Value

This method does not return a value.

Exception

  • NA

Example

The following example shows the usage of java.io.PrintWriter.close() 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 system
         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();
      }
   }
}

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

Hello World This is an example



Advertisements

Advertisements