
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - StringReader close() method
Description
The Java StringReader 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.
Declaration
Following is the declaration for java.io.StringReader.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 StringReader close() method
The following example shows the usage of StringReader close() method.
StringReaderDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.StringReader; public class StringReaderDemo { public static void main(String[] args) { String s = "Hello World"; // create a new StringReader StringReader sr = new StringReader(s); try { // read the first five chars for (int i = 0; i < 5; i++) { char c = (char) sr.read(); System.out.print("" + c); } // change the line System.out.println(); // close the stream sr.close(); //try to access the reader after it is closed try { sr.ready(); } catch (Exception ex) { // catch the exception and print a message if reader is closed System.out.println("Reader is closed."); } } catch (IOException ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
Hello Reader is closed.
Example - Properly closing a StringReader
The following example shows the usage of StringReader close() method.
StringReaderDemo.java
package com.tutorialspoint; import java.io.StringReader; import java.io.IOException; public class StringReaderDemo { public static void main(String[] args) throws IOException { StringReader reader = new StringReader("Hello, world!"); int data = reader.read(); System.out.println("First character: " + (char) data); reader.close(); // closing the reader System.out.println("Reader closed."); } }
Output
Let us compile and run the above program, this will produce the following result−
First character: H Reader closed.
Explanation
The reader reads one character successfully.
Then it's closed using close().
No error occurs because we don't attempt to read after closing.
Example - Reading after closing the StringReader
The following example shows the usage of StringReader close() method.
StringReaderDemo.java
package com.tutorialspoint; import java.io.StringReader; import java.io.IOException; public class StringReaderDemo { public static void main(String[] args) { StringReader reader = new StringReader("Java"); try { reader.close(); // Close the reader first int data = reader.read(); // Attempt to read after closing System.out.println("Read character: " + (char) data); } catch (IOException e) { System.out.println("Exception: " + e.getMessage()); } } }
Output
Let us compile and run the above program, this will produce the following result−
Exception: Stream closed
Explanation
Surprisingly, StringReader.read() still works even after close() (because it doesnât hold real I/O resources).
This behavior is allowed but not guaranteed across all reader types, so in practice, it's always best to avoid using a stream after it's closed.