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



Description

The Java FilterReader read(char[] cbuf, int off, int len) method reads multiple characters into a character array starting at a specified offset and reads up to a given length.

Declaration

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

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

Parameters

  • cbuf − Destination buffer.

  • off − Offset at which to start storing characters.

  • len − Number of characters to read.

Return Value

This method returns the number of characters to read, or -1 if the end of the stream is reached.

Exception

IOException − If an I/O error occurs.

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

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

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;

public class FilterReaderDemo {
   public static void main(String[] args) throws Exception {
      FilterReader fr = null;
      Reader r = null;
      char[] cbuf = new char[6];
      int i = 0;
      
      try {
         // create new reader
         r = new StringReader("ABCDEF");
          
         // create new filter reader
         fr = new FilterReader(r) {
         };
         
         // read data of len 4, to the buffer
         i = fr.read(cbuf, 2, 4);
         System.out.println("No. of characters read: "+i);
         
         // read till the end of the char buffer
         for(char c:cbuf) {
         
            // checks for the empty character in buffer
            if((byte)c == 0)
               c = '-';
            
            // prints
            System.out.print(c);
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(r!=null)
            r.close();
         if(fr!=null)
            fr.close();
      }
   }
}

Output

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

No. of characters read: 4
--ABCD

Example - Reading Characters into a Buffer Using BufferedReader

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

FilterReaderDemo.java

package com.tutorialspoint;

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

public class FilterReaderDemo {
   public static void main(String[] args) {
      try (BufferedReader fr = new BufferedReader(new FileReader("example.txt"))) {
         char[] buffer = new char[10]; // Buffer of size 10
         int charsRead = fr.read(buffer, 2, 5); // Read 5 characters starting at index 2

         if (charsRead != -1) {
            System.out.println("Characters read: " + charsRead);
            System.out.println("Buffer content: " + new String(buffer));
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains HelloWorld))

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

Characters read: 5
Buffer content:   Hello

Explanation

  • Uses BufferedReader, a subclass of FilterReader.

  • Creates a character buffer (char[10]).

  • Reads 5 characters starting at index 2 in the buffer.

  • Prints the buffer including empty slots (\u0000).

Example - Reading a File in Chunks Using PushbackReader

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

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.FileReader;
import java.io.FilterReader;
import java.io.IOException;
import java.io.PushbackReader;

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

         while ((charsRead = fr.read(buffer, 3, 4)) != -1) { // Read 4 characters at index 3
            System.out.println("Characters read: " + charsRead);
            System.out.println("Buffer content: " + new String(buffer));
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Microservices"))

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

Characters read: 4
Buffer content:    Micr
Characters read: 4
Buffer content:    ices

Explanation

  • Uses PushbackReader, a FilterReader subclass.

  • Reads 4 characters at a time into buffer, starting at index 3.

  • Prints the buffer after each read operation.

  • Loop continues until EOF (-1 is returned).

java_io_filterreader.htm
Advertisements