Java CharArrayReader read() Example
CharArrayReader class read() method example. This example shows you how to use read() method.
Syntax is : public int read() throws IOException
This method reads a single character.
Here is the code
/**
* @(#) ReadCharArrayReader.java
* A class representing use of method read() of CharArrayReader
class in java.io Package.
* @Version 31-May-2008
* @author Rose India Team
*/
import java.io.*;
public class ReadCharArrayReader {
public static void main(String[] args) throws IOException{
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);
// read() method call.
System.out.println("\nFirst 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());
}
}
/* 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
First character in buffer is : M
Next character in buffer is : A
Next character in buffer is : H |
|