Java - FilterReader close() method



Description

The Java FilterReader close() method closes the reader and releases system resources associated with it. Once closed, the reader cannot be used again. Frees system resources (e.g., file handles).

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 - Usage of FilterReader close() method

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

FilterReaderDemo.java

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");
         }
      }
   }
}

Output

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

Example - Using close() with BufferedReader (Automatic Closure)

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

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FilterReaderDemo {
   public static void main(String[] args) {
      try (BufferedReader fr = new BufferedReader(new FileReader("example.txt"))) {
         int data;
         while ((data = fr.read()) != -1) { // Read character by character
            System.out.print((char) data);
         }
         // No need to manually close, try-with-resources handles it
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Hello")

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

Hello

Explanation

  • Uses BufferedReader, a subclass of FilterReader, to read "example.txt".

  • Reads characters one at a time and prints them.

  • Uses try-with-resources, which automatically calls close().

Example - Manually Closing PushbackReader (Another FilterReader Subclass)

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

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.FileReader;
import java.io.FilterReader;
import java.io.IOException;
import java.io.PushbackReader;

public class FilterReaderDemo {
   public static void main(String[] args) {
      FilterReader fr = null;
      try {
         fr = new PushbackReader(new FileReader("example.txt"));

         int data = fr.read();
         System.out.println("First character read: " + (char) data);

      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         try {
            if (fr != null) {
               fr.close(); // Manually closing the stream
               System.out.println("Stream closed successfully.");
            }
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}

Output(if example.txt contains "Java")

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

First character read: J
Stream closed successfully.

Explanation

  • Uses PushbackReader, another FilterReader subclass.

  • Reads the first character and prints it.

  • Manually closes the reader inside finally, ensuring resources are freed.

  • Prevents memory leaks by checking if (fr != null) before calling close().

java_io_filterreader.htm
Advertisements