Java.io.FilterWriter.close() Method

Advertisements


Description

The java.io.FilterWriter.close() method closes the string flushing it first. Once the stream is closed, futher invocation of write() or flush() will throw IOException.

Declaration

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

public void close()

Parameters

  • NA

Return Value

The method does not return any value.

Exception

  • IOException -- If an I/O error occurs.

Example

The following example shows the usage of java.io.FilterWriter.close() method.

package com.tutorialspoint;

import java.io.FilterWriter;
import java.io.StringWriter;
import java.io.Writer;

public class FilterWriterDemo {
   public static void main(String[] args) throws Exception {
      
      FilterWriter fw = null;
      Writer w = null;
      String s=null;
      
      try{
         // create new reader
         w = new StringWriter(6);
          
         // filter writer
         fw = new FilterWriter(w) {
         };
         
         // write to filter writer
         fw.write(65);
         
         // get the string
         s = w.toString();
         
         // print
         System.out.println("String: "+s);
         
      }catch(Exception e){
         
         // if any I/O error occurs
         e.printStackTrace();
      }finally{
         
         // releases system resources associated with this stream
         if(w!=null)
            w.close();
         if(fw!=null)
         {
            fw.close();
            System.out.println("close() invoked");
            System.out.print("flushes out and then closes the stream");
         }
      }
   }
}

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

String: A
close() invoked
flushes out and then closes the stream



Advertisements
Advertisements