Java IO Tutorial - Java LineNumberReader.read(char[] cbuf, int off, int len)








Syntax

LineNumberReader.read(char[] cbuf, int off, int len) has the following syntax.

public int read(char[] cbuf, int off, int len)  throws IOException

Example

In the following code shows how to use LineNumberReader.read(char[] cbuf, int off, int len) method.

/*from w  ww .j  a v  a  2  s .  c  o  m*/
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class Main {
  public static void main(String[] args) throws IOException {

    char[] cbuf = new char[5];

    // create new reader
    FileReader fr = new FileReader("C:/test.txt");
    LineNumberReader lnr = new LineNumberReader(fr);

    // read characters into the buffer
    int i = lnr.read(cbuf, 2, 3);
    System.out.println("Number of char read: " + i);

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

      // prints char
      System.out.print(c);
    }
        lnr.close();
  }
}