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

JavaObject Oriented ProgrammingProgramming

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!
raja
Published on 06-Aug-2019 18:36:16
Advertisements