Java - LineNumberReader read(char[] cbuf, int off, int len) method



Description

The Java LineNumberReader read(char[] cbuf, int off, int len) method reads up to len characters into the character array cbuf, starting at index off. It tracks line numbers automatically while reading.

Declaration

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

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

Parameters

  • cbuf − Destination character buffer.

  • off − Offset at which to start storing characters.

  • len − Maximum number of characters to read.

Return Value

The method returns the number of bytes read, or -1 if the end of the stream has already been reached.

Exception

IOException − If an I/O error occurs.

Example - Usage of LineNumberReader read(char[] cbuf, int off, int len) method

The following example shows the usage of Java LineNumberReader read(char[] cbuf, int off, int len) method.

LineNumberReaderDemo.java

package com.tutorialspoint;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class LineNumberReaderDemo {
   public static void main(String[] args) throws IOException {
      FileReader fr = null;
      LineNumberReader lnr = null;
      int i;
      char[] cbuf = new char[5];
      
      try {
         // create new reader
         fr = new FileReader("test.txt");
         lnr = new LineNumberReader(fr);
         
         // read characters into the buffer
         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);
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // closes the stream and releases system resources
         if(fr!=null)
            fr.close();
         if(lnr!=null)
            lnr.close();
      }
   }
}

Output(Assuming test.txt contains "ABCDE")

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

Number of char read: 3
--ABC

Example - Reading a String in Chunks

The following example shows the usage of Java LineNumberReader read(char[] cbuf, int off, int len) method. This example reads a string in chunks of characters.

LineNumberReaderDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;

public class LineNumberReaderDemo {
   public static void main(String[] args) {
      String text = "Hello\nWorld\nJava Programming";
      LineNumberReader lnr = new LineNumberReader(new StringReader(text));

      try {
         char[] buffer = new char[10]; // Character buffer
         int bytesRead;

         while ((bytesRead = lnr.read(buffer, 0, buffer.length)) != -1) {
            System.out.println("Read: " + new String(buffer, 0, bytesRead));
            System.out.println("Current Line Number: " + lnr.getLineNumber());
         }

         lnr.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Read: Hello
Worl
Current Line Number: 1
Read: d
Java Pr
Current Line Number: 2
Read: ogramming
Current Line Number: 2

Explanation

  • A char[] buffer of size 10 is created to read characters in chunks.

  • The read(buffer, 0, buffer.length) method reads up to 10 characters at a time.

  • Line numbers increment when a newline (\n) is encountered.

  • The last chunk may contain fewer characters than the buffer size.

Example - Reading from a File with Offset

The following example shows the usage of Java LineNumberReader read(char[] cbuf, int off, int len) method. This example reads from a file and uses an offset (y) when storing characters into the buffer.

LineNumberReaderDemo.java

package com.tutorialspoint;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class LineNumberReaderDemo {
   public static void main(String[] args) {
      try {
         LineNumberReader lnr = new LineNumberReader(new FileReader("example.txt"));

         char[] buffer = new char[20]; // Buffer of size 20
         int bytesRead;

         while ((bytesRead = lnr.read(buffer, 5, 10)) != -1) { // Read into buffer starting at index 5
            System.out.println("Read: " + new String(buffer, 5, bytesRead));
            System.out.println("Current Line Number: " + lnr.getLineNumber());
         }

         lnr.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(If example.txt contains multiple lines)

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

Read: Line 1
Current Line Number: 1
Read: Line 2
Current Line Number: 2
Read: Line 3
Current Line Number: 3

Explanation

  • A 20-character buffer is created.

  • read(buffer, 5, 10) starts filling at index 5 and reads up to 10 characters.

  • The first 5 indices remain empty, preventing overwriting.

  • Line numbers increment automatically when \n is encountered.

java_io_linenumberreader.htm
Advertisements