Java - InputStreamReader close() method



Description

The Java InputStreamReader close() method is used to close the stream and release system resources associated with it. No more reading can be done after closing.

Declaration

Following is the declaration for java.io.InputStreamReader.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 - Usage of InputStreamReader close() method

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

InputStreamReaderDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputStreamReaderDemo {
   public static void main(String[] args) throws IOException {
      FileInputStream fis = null;
      InputStreamReader isr = null;
      int i;
      char c;
      
      try {
         // new input stream reader is created 
         fis = new FileInputStream("test.txt");
         isr = new InputStreamReader(fis);
         
         // input stream reader is closed
         isr.close();
         System.out.println("close() invoked");
         
         // read() called after closed method
         i = isr.read();
         c = (char)i;
         System.out.println(c);
      
      } catch (Exception e) {
         // print error
         System.out.println("The stream is already closed");
      } finally {
         // closes the stream and releases resources associated
         if(fis!=null)
            fis.close();
         if(isr!=null)
            isr.close();
      }   
   }
}

Output(Assuming test.txt contains "ABCDE")

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

close() invoked
The stream is already closed

Example - Using close() with InputStreamReader (Automatic Closure with Try-With-Resources)

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

InputStreamReaderDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputStreamReaderDemo {
   public static void main(String[] args) {
      try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt"))) {
         int data;
         while ((data = reader.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 "JavaProgramming")

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

JavaProgramming

Explanation

  • Uses InputStreamReader, which reads characters from a file.

  • Reads one character at a time using read().

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

Example - Manually Closing InputStreamReader in a finally Block

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

InputStreamReaderDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputStreamReaderDemo {
   public static void main(String[] args) {
      InputStreamReader reader = null;
      try {
         reader = new InputStreamReader(new FileInputStream("example.txt"));
         int data = reader.read();
         System.out.println("First character read: " + (char) data);
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         try {
            if (reader != null) {
	           reader.close(); // Manually closing the reader
	           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 InputStreamReader with FileInputStream to read a file.

  • Reads the first character and prints it.

  • Manually closes the reader inside finally to ensure resources are freed.

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

java_io_inputstreamreader.htm
Advertisements