Java.io.CharArrayReader.read() Method


Advertisements


Description

The java.io.CharArrayReader.read(char[] b, int off, int len) method reads character into portion of the specified array.

Declaration

Following is the declaration for java.io.CharArrayReader.read(char[] b, int off, int len) method −

public int read(char[] b, int off, int len)

Parameters

  • b − destination array.

  • off − Offset to start storing characters.

  • len − number of characters to read.

Return Value

The actual number of characters read, -1 for the end of the stream.

Exception

IOException − If any I/O error occurs.

Example

The following example shows the usage of java.io.CharArrayReader.read(char[] b, int off, int len) method.

package com.tutorialspoint;

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

public class CharArrayReaderDemo {

   public static void main(String[] args) {      CharArrayReader car = null;
      char[] ch = {'H', 'E', 'L', 'L', 'O'};
      char[] d = new char[5];
      
      try {
         
         // create new character array reader
         car = new CharArrayReader(ch);
         
         // read character to the destination buffer
         car.read(d, 3, 2);
         
         // for every character in the buffer
         for (char c : d) {
            int i = (int)c;
            
            if(i == 0) {
               System.out.println("0");
            } else {
               System.out.println(c);
            }
            
         }
      } catch(IOException e) {
         
         // if I/O error occurs
         System.out.print("Stream is already closed");
      } finally {
         
         // releases any system resources associated with the stream
         if(car!=null)
            car.close();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

0
0
0
H
E

java_io_chararrayreader.htm

Advertisements