Java.io.CharArrayReader Class in Java
The CharArrayReader class in Java is part of the java.io package and allows you to read characters from a character array (char[]) as a stream. It extends the Reader class and is mainly used when you want to treat a character array as an input source, similar to reading from files or other streams.
Class Declaration
public class CharArrayReader extends Reader
- Package: java.io
- Superclass: Reader
- Implements: Closeable, AutoCloseable
Constructors of CharArrayReader
CharArrayReader provides two constructors
- CharArrayReader(char[] char_array): Creates a CharArrayReader using the entire character array.
- CharArrayReader(char[] char_array, int offset, int maxlen): Creates a CharArrayReader starting from a specific offset and reading only maxlen characters.
Important Methods of CharArrayReader
1. read()
Reads a single character from the input stream and returns it as an integer
import java.io.*;
public class GFG{
public static void main(String[] args) throws IOException{
char[] data = {'J', 'A', 'V', 'A'};
CharArrayReader reader = new CharArrayReader(data);
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch + " ");
}
reader.close();
}
}
Output
J A V A
2. read(char[] buffer, int offset, int length)
Reads characters into a portion of the specified array from the given offset for a given length.
import java.io.*;
public class GFG{
public static void main(String[] args) throws IOException{
char[] source = {'H', 'E', 'L', 'L', 'O'};
char[] dest = new char[5];
CharArrayReader reader = new CharArrayReader(source);
// Reads 3 chars into dest starting from index 1
reader.read(dest, 1, 3);
System.out.println(dest);
reader.close();
}
}
Output
HEL
3. ready()
Checks whether the stream is ready to be read. Since it’s memory-based, it’s always ready unless closed.
import java.io.*;
public class GFG{
public static void main(String[] args) throws IOException{
char[] data = {'O', 'P', 'E', 'N', 'A', 'I'};
CharArrayReader reader = new CharArrayReader(data);
if (reader.ready()){
System.out.println("Reader is ready to read.");
}
reader.close();
}
}
Output
Reader is ready to read.
4. skip(long n)
Skips the specified number of characters in the stream.
import java.io.*;
public class GFG{
public static void main(String[] args) throws IOException{
char[] data = {'A', 'B', 'C', 'D', 'E'};
CharArrayReader reader = new CharArrayReader(data);
// Skip first two character
reader.skip(2);
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch + " ");
}
reader.close();
}
}
Output
C D E
5. mark(int readAheadLimit)
Marks the current position in the stream so it can be returned to later using reset().
import java.io.*;
public class GFG{
public static void main(String[] args) throws IOException{
char[] data = {'X', 'Y', 'Z', 'P', 'Q'};
CharArrayReader reader = new CharArrayReader(data);
System.out.print((char) reader.read()); // X
// Mark after reading 'X'
reader.mark(0);
System.out.print((char) reader.read()); // Y
System.out.print((char) reader.read()); // Z
// Go back to mark
reader.reset();
System.out.print("\nAfter reset: ");
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch + " ");
}
reader.close();
}
}
Output
XYZ After reset: Y Z P Q
6. markSupported()
Returns true indicating that the mark() and reset() methods are supported.
import java.io.*;
public class GFG{
public static void main(String[] args){
char[] data = {'A', 'I'};
CharArrayReader reader = new CharArrayReader(data);
System.out.println("markSupported(): " + reader.markSupported());
}
}
Output
markSupported(): true
7. reset()
Resets the stream to the most recently marked position or to the beginning if no mark was set.
import java.io.*;
public class GFG{
public static void main(String[] args) throws IOException{
char[] data = {'H', 'E', 'L', 'L', 'O'};
CharArrayReader reader = new CharArrayReader(data);
System.out.print((char) reader.read()); // H
// Mark position
reader.mark(0);
System.out.print((char) reader.read()); // E
System.out.print((char) reader.read()); // L
// Go back to mark
reader.reset();
System.out.print("\nAfter reset: ");
int ch;
while ((ch = reader.read()) != -1) {
System.out.print((char) ch + " ");
}
reader.close();
}
}
Output
HEL After reset: E L L O
8. close()
Closes the reader and releases resources.
import java.io.*;
public class GFG{
public static void main(String[] args) throws IOException{
char[] data = {'J', 'A', 'V', 'A'};
CharArrayReader reader = new CharArrayReader(data);
reader.close();
System.out.println("Reader closed successfully.");
}
}
Output
Reader closed successfully.