VOOZH about

URL: https://www.geeksforgeeks.org/java/reader-close-method-in-java-with-examples/

⇱ Reader close() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Reader close() method in Java with Examples

Last Updated : 24 Oct, 2025

In Java, the Reader class provides a way to read character streams. Its close() method is used to close the stream and release any associated resources.

  • If the stream is open, close() closes it and releases any associated resources.
  • If the stream is already closed, calling close() has no effect.
  • Any read or write operation attempted after closing the stream will throw an IOException.

Syntax

public abstract void close()

  • Parameters: This method does not accept any parameters
  • Return Type: This method does not return any value.

Examples of close() Method

Example 1: Reading Characters and Closing the Stream


Output
Integer value of character read: 71
Actual character read: G
Integer value of character read: 101
Actual character read: e
Integer value of character read: 101
Actual character read: e
Integer value o...

Explanation: The first 5 characters of the string "GeeksForGeeks" are read and printed. Calling close() releases the stream resources, marking the end of the reader’s usage.

Example 2: Performing Operations After Closing the Stream


Output
Stream Closed.
java.io.IOException: Stream closed

Explanation: After calling close(), any attempt to read from or interact with the stream (e.g., ready()) results in an IOException.

Why Use close()?

  • Prevents resource leaks in your program.
  • Ensures that files or network streams are properly released.
  • Mandatory in real-world applications to avoid memory and file descriptor exhaustion.

Note: Using try-with-resources in Java automatically calls close() on streams, making code safer and cleaner.


Comment