In this tutorial we will learn about the CharArrayReader class in Java.
CharArrayReader class in java.io package provides the facility to read the character input stream from char array. Input stream for this class is an array of characters.
Constructor of CharArrayReader
| CharArrayReader(char[] buf) | This constructor creates a CharaArrayReader with a specified char array data
source. Syntax : public CharArrayReader(char[] buf) |
| CharArrayReader(char[] buf, int offset, int length) | This constructor creates a CharArrayReader with the specified length of
character of a char array started from the specified offset 'off'. Syntax : public CharArrayReader(char[] buf, int offset, int length) |
Methods in CharArrayReader
Example
A simple example is being given here which demonstrates about how to use the java.io.CharArrayReader to read the stream of char array. In this example I have created a Java class to read the char array. In this class to read the char array creates a char array variable with some characters then passes this input stream to the constructor of CharArrayReader().
Source Code
JavaCharArrayReaderExample.java
import java.io.CharArrayReader;
import java.io.IOException;
public class JavaCharArrayReaderExample
{
public static void main(String args[])
{
char ch[] = {'a', 'b', 'c', 'd'};
CharArrayReader car = null;
try
{
car = new CharArrayReader(ch);
int c;
System.out.println();
System.out.println("Characters in char array");
while((c=car.read()) != -1)
{
System.out.print("'"+(char)c+"'"+" ");
}
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
if(car != null)
{
try
{
car.close();
}
catch(Exception ioe)
{
System.out.println(ioe);
}
}
}// end finally
}// end main
}// end class
Output

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Java IO CharArrayReader
Post your Comment