Java.io.FilterReader.close() Method



Description

The java.io.FilterReader.close() method closes the stream and releases any system resources associated with it. Once the stream is closed, further invocation to read(), ready(), mark(), reset() or skip() will throw an IOException.

Declaration

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

public void close()

Parameters

NA

Return Value

This method does not return any value.

Exception

IOException − If an I/O error occurs.

Example

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

package com.tutorialspoint;

import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class FilterReaderDemo {
   public static void main(String[] args) throws Exception {
      FilterReader fr = null;
      Reader r = null;
      int i = 0;
      char c;
            
      try {
         // create new reader
         r = new StringReader("ABCDEFG");
          
         // create new filter reader
         fr = new FilterReader(r) {
         };
         
         // read till the end of the stream
         while((i = fr.read())!=-1) {
            c = (char)i;
            System.out.println(c);
         }
         
      } catch(IOException e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources
         if(r!=null)
            r.close();
         if(fr!=null) {
            fr.close();
            System.out.print("Stream is closed");
            System.out.print(" system resources released");
         }
      }
   }
}

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

A
B
C
D
E
F
G
Stream is closed system resources released
java_io_filterreader.htm
Advertisements