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



Description

The Java FileReader read(char[] cbuf, int off, int len) method reads characters from a file into a character array. Efficient for large files since it avoids loading the entire content at once.

Declaration

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

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

Parameters

  • cbuf− The character array where data will be stored.

  • off− The starting position in the array to store characters.

  • len− The number of characters to read.

Return Value

This method returns the actual number of characters read (-1 if end of file is reached).

Exception

IOException − If an I/O error occurs.

Example - Reading a Fixed Number of Characters

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

FileReaderDemo.java

package com.tutorialspoint;

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

public class FileReaderDemo {
   public static void main(String[] args) {
      try (FileReader fr = new FileReader("example.txt")) {
         char[] buffer = new char[10]; // Buffer of size 10

         // Read 5 characters into buffer, starting at index 0
         int charsRead = fr.read(buffer, 0, 5);

         System.out.println("Characters read: " + new String(buffer, 0, charsRead));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Hello World")

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

Characters read: Hello

Explanation

  • Opens example.txt using FileReader.

  • Reads 5 characters into buffer from index 0.

  • Prints the characters read.

Example - Reading Characters with an Offset

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

FileReaderDemo.java

package com.tutorialspoint;

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

public class FileReaderDemo {
   public static void main(String[] args) {
      try (FileReader fr = new FileReader("example.txt")) {
         char[] buffer = new char[10]; // Buffer of size 10

         // Read 5 characters and store them starting at index 2
         int charsRead = fr.read(buffer, 0, 5);

         System.out.println("Buffer content: " + new String(buffer));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "JavaProgramming")

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

Buffer content:   JavaP

Explanation

  • Reads 5 characters and stores them starting at index 0 in the buffer.

Example - Reading Large Files in Chunks

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

FileReaderDemo.java

package com.tutorialspoint;

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

public class FileReaderDemo {
   public static void main(String[] args) {
      try (FileReader fr = new FileReader("largefile.txt")) {
         char[] buffer = new char[20]; // Read in chunks
         int charsRead;

         while ((charsRead = fr.read(buffer, 0, buffer.length)) != -1) {
            System.out.print(new String(buffer, 0, charsRead));
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

<Contents of the file largefile.txt>

Explanation

  • Reads 20 characters at a time in a loop.

  • Keeps reading until read() returns -1 (end of file).

  • Prints content chunk by chunk.

java_io_filereader.htm
Advertisements