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


The CharArrayReader is a subclass of Reader class and it can implement a character buffer that can be used as a character input stream. The CharArrayReader reads characters from a character array either completely or partially starting from an offset. The important methods of a CharArrayReader class are close(), mark(), read(), skip() and reset().

Syntax

public class CharArrayReader extends Reader

Example

import java.io.*;
public class CharArrayReaderTest {
   public static void main(String args[]) throws Exception {
      char array[] = { 'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', ' ', 'P', 'o', 'i', 'n', 't', '!'};
   CharArrayReader car = new CharArrayReader(array);
   BufferedReader br = new BufferedReader(car);
   String line;
   while ((line = br.readLine()) != null) {
      System.out.println(line);
   }
}

Output

Tutorials Point!

Updated on: 11-Feb-2020

249 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements