Java CharArrayReader Class
Last modified: April 16, 2025
The java.io.CharArrayReader
class implements a character stream
that reads from a character array. It's useful when you need to treat an
in-memory character array as a reader. This class is part of Java's I/O
package.
CharArrayReader
extends Reader
and provides methods
for reading characters from an array. The stream maintains an internal position
marker. It supports mark and reset operations for re-reading data.
CharArrayReader Class Overview
CharArrayReader
provides efficient character reading from arrays.
Key methods include read operations, mark/reset functionality, and stream
skipping. The class doesn't require external synchronization for thread safety.
public class CharArrayReader extends Reader { public CharArrayReader(char[] buf); public CharArrayReader(char[] buf, int offset, int length); public int read(); public int read(char[] b, int off, int len); public long skip(long n); public boolean ready(); public boolean markSupported(); public void mark(int readAheadLimit); public void reset(); public void close(); }
The code above shows key methods provided by CharArrayReader
.
These methods allow reading characters from an array efficiently. The class
supports mark and reset operations for revisiting previously read data.
Creating a CharArrayReader
CharArrayReader can be created from a full character array or a portion of it. The constructor parameters specify the source array and optional offset/length. The reader doesn't modify the original array.
import java.io.CharArrayReader; import java.io.IOException; public class Main { public static void main(String[] args) { char[] data = {'J', 'a', 'v', 'a', ' ', 'I', '/', 'O'}; // Create from full array try (CharArrayReader reader1 = new CharArrayReader(data)) { System.out.println("Reader 1 created from full array"); } catch (IOException e) { e.printStackTrace(); } // Create from portion of array try (CharArrayReader reader2 = new CharArrayReader(data, 2, 4)) { System.out.println("Reader 2 created from array portion"); } catch (IOException e) { e.printStackTrace(); } } }
This example demonstrates different ways to create CharArrayReader. The first uses the full array, while the second reads only a portion. The try-with-resources statement ensures proper stream closure. IOException handling is included.
Reading Characters One by One
The simplest way to read from CharArrayReader is using the read() method. It returns one character at a time as an int. The method returns -1 at end of stream. This approach is useful for processing each character individually.
import java.io.CharArrayReader; import java.io.IOException; public class Main { public static void main(String[] args) { char[] data = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'}; try (CharArrayReader reader = new CharArrayReader(data)) { int charValue; while ((charValue = reader.read()) != -1) { System.out.print((char) charValue); } System.out.println("\nReading complete"); } catch (IOException e) { e.printStackTrace(); } } }
This example reads characters one by one from the array. The while loop continues until read() returns -1. Each character is cast to char before printing. The output shows all characters from the array. IOException handling is included.
Reading Multiple Characters at Once
For better performance, read multiple characters into an array at once. The read(char[] cbuf, int off, int len) method allows this. It returns the number of characters actually read. This reduces method call overhead.
import java.io.CharArrayReader; import java.io.IOException; public class Main { public static void main(String[] args) { char[] source = "The quick brown fox jumps over the lazy dog".toCharArray(); char[] buffer = new char[10]; try (CharArrayReader reader = new CharArrayReader(source)) { int charsRead; while ((charsRead = reader.read(buffer, 0, buffer.length)) != -1) { System.out.println("Read " + charsRead + " chars: " + new String(buffer, 0, charsRead)); } } catch (IOException e) { e.printStackTrace(); } } }
This example reads 10 characters at a time into a buffer. The actual number read may be less than requested at the end. The String constructor converts the buffer portion to a string. The output shows each chunk of characters read.
Using Mark and Reset
CharArrayReader supports mark/reset operations to re-read data. The mark method sets a position to return to. The reset method returns to this position. The readAheadLimit parameter specifies how far ahead can be read before mark becomes invalid.
import java.io.CharArrayReader; import java.io.IOException; public class Main { public static void main(String[] args) { char[] data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); try (CharArrayReader reader = new CharArrayReader(data)) { // Read first 5 characters for (int i = 0; i < 5; i++) { System.out.print((char) reader.read()); } // Mark current position reader.mark(10); System.out.println("\nMark set after 5 characters"); // Read next 5 characters for (int i = 0; i < 5; i++) { System.out.print((char) reader.read()); } // Reset to marked position reader.reset(); System.out.println("\nReset to mark position"); // Read again from marked position for (int i = 0; i < 5; i++) { System.out.print((char) reader.read()); } } catch (IOException e) { e.printStackTrace(); } } }
This example demonstrates mark/reset functionality. After reading 5 characters, we set a mark. After reading 5 more, we reset to the mark position. The output shows the same characters being read twice. The mark remains valid as we didn't exceed the readAheadLimit.
Skipping Characters
The skip method allows skipping a specified number of characters. This is more efficient than reading and discarding. The method returns the actual number of characters skipped. This may be less than requested at end of stream.
import java.io.CharArrayReader; import java.io.IOException; public class Main { public static void main(String[] args) { char[] data = "0123456789abcdefghij".toCharArray(); try (CharArrayReader reader = new CharArrayReader(data)) { System.out.println("Initial content: " + new String(data)); // Skip first 10 characters long skipped = reader.skip(10); System.out.println("Skipped " + skipped + " characters"); // Read next character System.out.println("Next character: " + (char) reader.read()); // Try to skip beyond end skipped = reader.skip(20); System.out.println("Skipped " + skipped + " characters (end approached)"); } catch (IOException e) { e.printStackTrace(); } } }
This example shows character skipping in action. We first skip 10 characters, then read one. The second skip attempts to go beyond the array end. The output shows the actual number skipped. The skip method is useful for ignoring portions of data.
Checking Readiness
The ready method checks if the reader can be read without blocking. For CharArrayReader, this always returns true if characters remain. It's useful for consistency with other Reader implementations.
import java.io.CharArrayReader; import java.io.IOException; public class Main { public static void main(String[] args) { char[] data = "ReadyCheck".toCharArray(); try (CharArrayReader reader = new CharArrayReader(data)) { System.out.println("Reader ready initially? " + reader.ready()); // Read all characters while (reader.ready()) { System.out.print((char) reader.read()); } System.out.println("\nReader ready after reading all? " + reader.ready()); } catch (IOException e) { e.printStackTrace(); } } }
This example demonstrates the ready method. Initially, the reader is ready. After reading all characters, ready returns false. The output shows the state changes. This method is mainly used for compatibility with other Reader types.
Source
Java CharArrayReader Class Documentation
In this article, we've covered the essential methods and features of the Java CharArrayReader class. Understanding these concepts is crucial for working with in-memory character data as streams in Java applications.
Author
List all Java tutorials.