Java CharArrayReader close() Example
CharArrayReader class close() method example. This example shows you how to use close() method.
Syntax is : public void close()
This 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. Closing a previously closed stream has no effect.
Here is the code.
/**
* @(#) CloseCharArrayReader.java
* A class representing use of method close() of CharArrayReader
class in java.io Package.
* @Version 31-May-2008
* @author Rose India Team
*/
import java.io.*;
public class CloseCharArrayReader {
public static void main(String[] args){
try{
char[] buffer = {'M','A','H','E','N','D','R','A'};
// Create object of CharArrayReader class
CharArrayReader obj = new CharArrayReader(buffer);
System.out.println("First character in buffer is : "+(char)obj.read());
System.out.println("Next character in buffer is : "+(char)obj.read());
System.out.println("Next character in buffer is : "+(char)obj.read());
// close() method call.
obj.close();
System.out.println("close() method call.");
System.out.println("Next character in buffer is : "+(char)obj.read());
}
catch (IOException e) {
System.out.println("Unable to read because Charcater buffer "
+"is close now.");
}
}
}
/* Another constructor of class CharArrayReader:-
public CharArrayReader(char[] buf, int offset, int length)
Description:
Creates a CharArrayReader from the specified array of chars.
The resulting reader will start reading at the given offset. The total
number of char values that can be read from this reader will be either
length or buf.length-offset, whichever is smaller.
Parameters:
buf - Input buffer (not copied)
offset - Offset of the first char to read
length - Number of chars to read
Throws:
IllegalArgumentException - If offset is negative or greater than
buf.length, or if length is negative, or if the sum of these two values
is negative. */ |
Output of the program.
First character in buffer is : M
Next character in buffer is : A
Next character in buffer is : H
close() method call.
Unable to read because Charcater buffer is close now. |
|