Java IO Tutorial - Java InputStreamReader.read(char[] cbuf, int offset, int length)








Syntax

InputStreamReader.read(char[] cbuf, int offset, int length) has the following syntax.

public int read(char[] cbuf, int offset, int length)  throws IOException

Example

In the following code shows how to use InputStreamReader.read(char[] cbuf, int offset, int length) method.

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/*from ww  w.j  a v  a2  s  .c  o  m*/
public class Main {
  public static void main(String[] args) throws IOException {
    char[] cbuf = new char[5];

    FileInputStream fis = new FileInputStream("C:/test.txt");
    InputStreamReader isr = new InputStreamReader(fis);

    // reads into the char buffer
    int i = isr.read(cbuf, 2, 3);

    // prints the number of characters
    System.out.println("Number of characters read: " + i);

    // for each character in the character buffer
    for (char c : cbuf) {
      // for empty character
      if (((int) c) == 0)
        c = '-';

      System.out.println(c);
    }
    isr.close();
  }
}