![]() |
VOOZH | about |
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.
public abstract void close()
Example 1: Reading Characters and Closing the Stream
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
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.
Note: Using try-with-resources in Java automatically calls close() on streams, making code safer and cleaner.