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 - CharArrayReader close() method
Selected Reading
Java - CharArrayReader close() methodDescriptionThe Java CharArrayReader close() method is used to close the stream and release any system resources associated with it. Once a CharArrayReader is closed, subsequent read or mark/reset operations on it will throw an IOException. However, since CharArrayReader operates on an in-memory character array and does not use external resources, closing it is more of a formality. DeclarationFollowing is the declaration for java.io.CharArrayReader.close() method − public void close() ParametersNA Return ValueThe method doesn't return any value. ExceptionNA Example - Using CharArrayReader close() methodThe following example shows the usage of Java CharArrayReader close() method. CharArrayReaderDemo.javapackage com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
CharArrayReader car = null;
char[] ch = {'H', 'E', 'L', 'L', 'O'};
try {
// create new character array reader
car = new CharArrayReader(ch);
// closes the character array stream
car.close();
// read the character array stream
car.read();
} catch(IOException e) {
// if I/O error occurs
System.out.print("Stream is already closed");
} finally {
// releases any system resources associated with the stream
if(car!=null)
car.close();
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Stream is already closed Example - Closing the Stream After UseThe following example shows the usage of Java CharArrayReader close() method. CharArrayReaderDemo.javapackage com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
char[] data = "Hello, World!".toCharArray();
// Create a CharArrayReader
CharArrayReader reader = new CharArrayReader(data);
try {
// Read and print characters
System.out.println("Reading characters:");
int charData;
while ((charData = reader.read()) != -1) {
System.out.print((char) charData);
}
// Close the reader
reader.close();
System.out.println("\nCharArrayReader closed.");
} catch (IOException e) {
System.err.println("An IOException occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Reading characters: Hello, World! CharArrayReader closed. Explanation
Example - Handling a Closed ReaderThe following example shows the usage of Java CharArrayReader close() method. CharArrayReaderDemo.javapackage com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
char[] data = "Java Programming".toCharArray();
// Create a CharArrayReader
CharArrayReader reader = new CharArrayReader(data);
try {
// Read a few characters
System.out.println("Reading some characters:");
for (int i = 0; i < 5; i++) {
System.out.print((char) reader.read());
}
System.out.println();
// Close the reader
reader.close();
System.out.println("CharArrayReader closed.");
// Attempt to read after closing the stream
System.out.println("Trying to read after closing:");
int charData = reader.read();
if (charData == -1) {
System.out.println("End of stream reached (or reader is closed).");
}
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Reading some characters: Java CharArrayReader closed. Trying to read after closing: IOException: Stream closed Explanation
Key Points
java_io_chararrayreader.htm Advertisements
|
| You are visitor 01402503 — 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. |