Java - Reader close() method



Description

The Java Reader close() method closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException.

close() method −

  • Closes the stream and releases system resources associated with it.

  • After calling close(), the Reader is no longer usable.

  • Always call close() when done with reading, to avoid memory leaks or file locks.

  • Often used in a try-with-resources block (Java 7+), which auto-closes the stream.

Declaration

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

public abstract void close()

Parameters

NA

Return Value

This method does not return a value.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of Reader close() method

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

ReaderDemo.java

package com.tutorialspoint;

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

public class ReaderDemo {
   public static void main(String[] args) {
      String s = "Hello World";

      // create a new StringReader
      Reader reader = new StringReader(s);

      try {
         // read the first five chars
         for (int i = 0; i < 5; i++) {
            char c = (char) reader.read();
            System.out.print( c);
         }

         // change line
         System.out.println();

         // close the stream
         System.out.println("Closing Stream...");
         reader.close();
         System.out.println("Stream Closed.");

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

Hello
Closing Stream...
Stream Closed.

Example - Using close() Manually with StringReader

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

ReaderDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.StringReader;

public class ReaderDemo {
   public static void main(String[] args) {
      String data = "This is a sample string.";
      StringReader reader = null;

      try {
         reader = new StringReader(data);

         int ch;
         while ((ch = reader.read()) != -1) {
            System.out.print((char) ch);
         }

      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         if (reader != null) {
            reader.close();
            System.out.println("\nReader closed successfully.");
         }
      }
   }
}

Output

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

This is a sample string.
Reader closed successfully.

Explanation

  • Opens a file sample.txt for reading.

  • Reads characters one by one.

  • close() is called in the finally block to ensure it's always executed.

Example - Using close() Automatically with Try-with-Resources

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

ReaderDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class ReaderDemo {
   public static void main(String[] args) {
      String data = "Line 1\nLine 2\nLine 3";

      // Use try-with-resources to auto-close the reader
      try (BufferedReader reader = new BufferedReader(new StringReader(data))) {
         String line;
         while ((line = reader.readLine()) != null) {
            System.out.println(line);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Line 1
Line 2
Line 3

Explanation

  • Uses BufferedReader (which is a subclass of Reader) with try-with-resources.

  • Java automatically calls close() at the end of the try block.

  • Cleaner and safer way to manage resources.

java_io_reader.htm
Advertisements