GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - StringReader close() method
Selected Reading
Java - StringReader close() methodDescriptionThe 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. DeclarationFollowing is the declaration for java.io.StringReader.close() method. public abstract void close() ParametersNA Return ValueThis method does not return a value.
ExceptionIOException − If an I/O error occurs. Example - Usage of StringReader close() methodThe following example shows the usage of StringReader close() method. StringReaderDemo.javapackage 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();
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Hello Reader is closed. Example - Properly closing a StringReaderThe following example shows the usage of StringReader close() method. StringReaderDemo.javapackage 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.");
}
}
OutputLet us compile and run the above program, this will produce the following result− First character: H Reader closed. Explanation
Example - Reading after closing the StringReaderThe following example shows the usage of StringReader close() method. StringReaderDemo.javapackage 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());
}
}
}
OutputLet us compile and run the above program, this will produce the following result− Exception: Stream closed Explanation
java_io_stringreader.htm Advertisements
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |