Java CharArrayReader ready() Example
CharArrayReader class ready() method example. This example shows you how to use ready() method.
Syntax is : public boolean ready() throws IOException
This method tells whether this stream is ready to be read. Character-array readers are always ready to be read.
Here is the code.
/**
* @(#) ReadyCharArrayReader.java
* A class representing use of method ready() of CharArrayReader
class in java.io Package.
* @Version 31-May-2008
* @author Rose India Team
*/
import java.io.*;
public class ReadyCharArrayReader {
public static void main(String[] args) throws IOException{
try{
char[] buffer = {'M','A','H','E','N','D','R','A'};
System.out.print("given character buffer is : ");
for (int i=0;i<buffer.length ;i++ ){
System.out.print(buffer[i]);
}
// Create object of CharArrayReader class
CharArrayReader obj = new CharArrayReader(buffer);
// ready() method call.
boolean bool = obj.ready();
System.out.println("\nThis stream is ready to be read...? " + bool);
obj.close();
System.out.println("Buffer stream is close now.");
bool = obj.ready();
}
catch (IOException e) {
System.out.println("Charcater buffer is not ready to read.");
}
}
}
/* 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.
given character buffer is : MAHENDRA
This stream is ready to be read...? true
Buffer stream is close now.
Charcater buffer is not ready to read. |
|