Java - Reader read(char[] cbuf,int off,int len) method
Description
The Java Reader read(char[] cbuf,int off,int len) method reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.
Declaration
Following is the declaration for java.io.Reader.read(char[] cbuf,int off,int len) method.
public abstract int read(char[] cbuf,int off,int len)
Parameters
cbuf − Destination buffer.
off − Offset at which to start storing 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 some I/O error occurs.
Example - Usage of Reader read(char[] cbuf,int off,int len) method
The following example shows the usage of Reader read(char[] cbuf,int off,int len) method.
ReaderDemo.java
package com.tutorialspoint;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
public class ReaderDemo {
public static void main(String[] args) {
String s = "Hello world";
// create a StringReader
Reader 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.
System.out.println( 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 −
5 Hello
Example - Reading into a portion of a character array
The following example shows the usage of Reader read(char[] cbuf,int off,int len) method.
ReaderDemo.java
package com.tutorialspoint;
import java.io.StringReader;
import java.io.IOException;
public class ReaderDemo {
public static void main(String[] args) {
String data = "ABCDEFG";
try (StringReader reader = new StringReader(data)) {
char[] buffer = new char[10];
// Fill initial buffer to show unused areas later
for (int i = 0; i < buffer.length; i++) {
buffer[i] = '-';
}
int charsRead = reader.read(buffer, 2, 5); // Start at index 2, read 5 characters
System.out.println("Characters read: " + charsRead);
System.out.println("Buffer content: " + new String(buffer));
} 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: --ABCDE---
Explanation
We read 5 characters starting at index 2 in the buffer.
Unused positions (like index 0 and 1) remain as '-' to visualize where data was placed.
Example - Multiple reads with different offsets
The following example shows the usage of Reader read(char[] cbuf,int off,int len) method.
ReaderDemo.java
package com.tutorialspoint;
import java.io.StringReader;
import java.io.IOException;
public class ReaderDemo {
public static void main(String[] args) {
String data = "123456789";
try (StringReader reader = new StringReader(data)) {
char[] buffer = new char[10];
int read1 = reader.read(buffer, 0, 4); // Fill from index 0
int read2 = reader.read(buffer, 5, 3); // Fill from index 5
System.out.println("First read (" + read1 + " chars): " + new String(buffer, 0, read1));
System.out.println("Second read (" + read2 + " chars): " + new String(buffer, 5, read2));
System.out.println("Full buffer: " + new String(buffer));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
Let us compile and run the above program, this will produce the following result−
First read (4 chars): 1234 Second read (3 chars): 567 Full buffer: 1234567
Explanation
First, we read 4 characters into the beginning of the buffer.
Then, we read the next 3 characters into a different part of the buffer (index 5).