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



Description

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

read(char[] cbuf,int off,int len) method −

  • Reads characters into a portion of a character array

  • Returns the number of characters actually read, or -1 if end of stream.

Declaration

Following is the declaration for java.io.PushbackReader.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 number of characters read, or -1 if the end of the stream has been reached.

Exception

IOException − If an I/O error occurs.

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

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

PushbackReaderDemo.java

 package com.tutorialspoint; import java.io.IOException; import java.io.PushbackReader; import java.io.StringReader; public class PushbackReaderDemo { public static void main(String[] args) { String s = "Hello World"; // create a new StringReader StringReader sr = new StringReader(s); // create a new PushBack reader based on our string reader PushbackReader pr = new PushbackReader(sr, 20); // create a char array to read chars into char cbuf[] = new char[5]; try { // read characters into an array. System.out.println("" + pr.read(cbuf)); // print cbuf System.out.println(cbuf); // Close the stream pr.close(); } catch (IOException ex) { ex.printStackTrace(); } } } 

Output

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

 5 Hello 

Example - Basic use of read(char[], int, int)

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

PushbackReaderDemo.java

 package com.tutorialspoint; import java.io.IOException; import java.io.PushbackReader; import java.io.StringReader; public class PushbackReaderDemo { public static void main(String[] args) { try (PushbackReader reader = new PushbackReader(new StringReader("HelloWorld"))) { char[] buffer = new char[10]; // Read 5 characters starting at index 2 in the array int charsRead = reader.read(buffer, 2, 5); System.out.println("Characters read: " + charsRead); System.out.print("Buffer content: "); for (char c : buffer) { System.out.print(c == '\u0000' ? '_' : c); // Print underscores for unused } } catch (IOException e) { e.printStackTrace(); } } } 

Output

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

 Characters read: 5 Buffer content: __Hello____ 

Explanation

  • Reads 5 characters ("Hello") into the array starting from index 2.

  • Other parts of the array remain with default \u0000 (null characters).

Example - Using read() after unread()

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

PushbackReaderDemo.java

 package com.tutorialspoint; import java.io.IOException; import java.io.PushbackReader; import java.io.StringReader; public class PushbackReaderDemo { public static void main(String[] args) { // Set pushback buffer size to 6 (more than 3 needed) try (PushbackReader reader = new PushbackReader(new StringReader("Stream"), 6)) { char[] buffer = new char[6]; // Read first 3 characters: 'S', 't', 'r' int read1 = reader.read(buffer, 0, 3); System.out.println("First read: " + new String(buffer, 0, read1)); // Str // Unread the same 3 characters reader.unread(buffer, 0, read1); // Read up to 6 characters again int read2 = reader.read(buffer, 0, 6); System.out.println("After unread, read again: " + new String(buffer, 0, read2)); // Stream } catch (IOException e) { e.printStackTrace(); } } } 

Output

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

 First read: Str After unread, read again: Strea 

Explanation

  • Reads "Str" into the buffer.

  • Then unreads it, placing it back into the stream.

  • Reads again − this time it continues beyond the previous read.

java_io_pushbackreader.htm
Advertisements