How can we convert character array to a Reader in Java?



Converting a character array to a reader in Java means treating the array as a stream of characters to be read sequentially. Let's quickly learn about character array and its creation in Java:

In Java, a character array is an array that holds elements of only char type, and it is also used to store a sequence of characters. Following is the syntax to define a character array in Java:

char char_array_name[] = {'char1', 'char2', 'char3'....}
or
char char_array_name[] = new char[size];

Here,

  • char_array_name: The name of the character array.
  • char_values: These are the char type values that the array holds.
  • size: The number of elements that the character array can hold.

Converting Character Array to Reader using CharArrayReader

To convert a character array to a reader in Java we have a class named Reader. In Java, the Reader class is an abstract class that works as the basis for reading a character stream. We can use its subclass CharArrayReader while converting the character array to a reader process.

The CharArrayReader can implement a character buffer that can be used as a character input stream. This class reads characters from a character array either completely or partially starting from an offset.

Syntax

Following is the syntax for creating a CharArrayReader class:

public class CharArrayReader extends Reader

Here, the CharArrayReader class extends on Reader, because the Reader is the parent class of it.

Example

In the following program, we use the CharArrayReader class to treat a character array as a stream and use the BufferedReader class to read and print its content line by line:

import java.io.BufferedReader;
import java.io.CharArrayReader;

public class CharToReader {
   public static void main(String args[]) throws Exception {
      //creating a character array with initial values
      char array[] = { 'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', ' ', 'P', 'o', 'i', 'n', 't'};
      System.out.println("The character array elements are: ");
      for(int i = 0; i<array.length; i++){
         System.out.print(array[i] + " ");
      }
      CharArrayReader car = new CharArrayReader(array);
      BufferedReader br = new BufferedReader(car);
      String line;
      System.out.println("\nAfter converting character array to a reader: ");
      while ((line = br.readLine()) != null) {
         System.out.println(line);
      }
   }
}

The above program displays the following output:

The character array elements are: 
T u t o r i a l s   P o i n t 
After converting character array to a reader:
Tutorials Point

Example 2

This is another similar example of converting a character array to a reader using the CharArrayReader class. We haven't used the BufferedReader class; instead, we directly read characters from the CharArrayReader using its read() method in a loop:

import java.io.CharArrayReader;
import java.io.IOException;

public class CharToReader {
    public static void main(String args[]) throws Exception {
        //creating a character array
      char[] charArray = {'T', 'u', 't', 'o', 'r', 'i', 'x'};
      System.out.println("The character array is: ");
      for(int i = 0; i<charArray.length; i++){
        System.out.print(charArray[i] + " ");
      }
      //using CharArrayReader class
        try (CharArrayReader reader = new CharArrayReader(charArray)) {
            int value;
            System.out.println("\nAfter converting to a reader: ");
            while ((value = reader.read()) != -1) {
                char character = (char) value;
                System.out.print(character);
            }
        } catch (IOException e) {
            e.printStackTrace();
     }
   }
}

Following is the output of the above program:

The character array is: 
T u t o r i x 
After converting to a reader:
Tutorix
Updated on: 2025-08-28T16:19:10+05:30

419 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements