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



Description

The Java StringReader read(char[] cbuf,int off,int len) method reads characters into a portion of an array.

Declaration

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

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

Parameters

  • cbuf − Destination buffer.

  • off − Offset at which to start writing characters.

  • len − Maximum number of characters to read.

Return Value

This method returns The character read, or -1 if the end of the stream has been reached.

Exception

IOException − If an I/O error occurs

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

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

StringReaderDemo.java

package com.tutorialspoint;

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

public class StringReaderDemo {
   public static void main(String[] args) {
      String s = "Hello world";

      // create a StringReader
      StringReader reader = new StringReader(s);

      // create a char array to read chars into
      char cbuf[] = new char[5];

      try {
         // read characters into a portion of an array.
         reader.read(cbuf, 0, 5);

         // print cbuf
         System.out.println(cbuf);

         // Close the stream 
         reader.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

Hello

Example - Read 4 characters into a buffer starting from index 1

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

StringReaderDemo.java

package com.tutorialspoint;

import java.io.StringReader;

public class StringReaderDemo {
   public static void main(String[] args) throws Exception {
      StringReader reader = new StringReader("Hello");
      char[] buffer = new char[6]; // larger than needed

      int charsRead = reader.read(buffer, 1, 4); // store from index 1

      System.out.println("Characters read: " + charsRead);
      System.out.print("Buffer content: ");
      for (char ch : buffer) {
         System.out.print(ch == '\u0000' ? '_' : ch); // show '_' for empty slots
      }

      reader.close();
   }
}

Output

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

Characters read: 4
Buffer content: _Hell_

Explanation

  • Reads 4 characters from "Hello" and stores them starting at buffer[1].

  • The first and last elements of the buffer remain default (\u0000), shown as _.

Example - Read the entire stream in chunks using offset and length

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

StringReaderDemo.java

package com.tutorialspoint;

import java.io.StringReader;

public class StringReaderDemo {
   public static void main(String[] args) throws Exception {
      StringReader reader = new StringReader("abcdef");
      char[] buffer = new char[3];

      int n;
      while ((n = reader.read(buffer, 0, buffer.length)) != -1) {
         System.out.print(new String(buffer, 0, n) + " ");
      }

      reader.close();
   }
}

Output

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

abc def 

Explanation

  • The stream "abcdef" is read in chunks of 3 characters using the buffer.

  • Each chunk is converted to a string and printed.

  • Shows how to loop with read(char[], int, int) for partial reads.

java_io_stringreader.htm
Advertisements