
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - InputStreamReader read(char[] cbuf, int offset, int length) method
Description
The Java InputStreamReader read(char[] cbuf, int offset, int length) method reads multiple characters into a character array (cbuf), starting from a given offset (off) and reading up to len characters. Returns the actual number of characters read (-1 if EOF is reached).
Declaration
Following is the declaration for java.io.InputStreamReader.read(char[] cbuf, int offset, int length) method −
public int read(char[] cbuf, int offset, int length)
Parameters
cbuf − Destination character buffer.
offset − Offset at which to start storing characters.
length − Maximum numbers of characters to read.
Return Value
The method returns the number of characters read, else -1 if the end of the stream has been reached.
Exception
IOException − If an I/O error occurs.
Example - Usage of InputStreamReader read(char[] cbuf, int offset, int length) method
The following example shows the usage of Java InputStreamReader read(char[] cbuf, int offset, int length) method.
InputStreamReaderDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReaderDemo { public static void main(String[] args) throws IOException { InputStreamReader isr = null; FileInputStream fis = null; char[] cbuf = new char[5]; int i; try { // new input stream reader is created fis = new FileInputStream("test.txt"); isr = new InputStreamReader(fis); // reads into the char buffer i = isr.read(cbuf, 2, 3); // prints the number of characters System.out.println("Number of characters read: "+i); // for each character in the character buffer for(char c:cbuf) { // for empty character if(((int)c) == 0) c = '-'; // prints the characters System.out.println(c); } } catch (Exception e) { // print error e.printStackTrace(); } finally { // closes the stream and releases resources associated if(fis!=null) fis.close(); if(isr!=null) isr.close(); } } }
Output(Assuming test.txt contains "ABCDE")
Let us compile and run the above program, this will produce the following result−
Number of characters read: 3 - - A B C
Example - Reading a Portion of a File Using InputStreamReader
The following example shows the usage of Java InputStreamReader read(char[] cbuf, int offset, int length) method.
InputStreamReaderDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReaderDemo { public static void main(String[] args) { try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt"))) { char[] buffer = new char[10]; // Character buffer of size 10 int charsRead = reader.read(buffer, 2, 5); // Read 5 characters, store from index 2 if (charsRead != -1) { System.out.println("Characters read: " + charsRead); System.out.println("Buffer content: " + new String(buffer)); // Print full 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 InputStreamReader to read from "example.txt".
Creates a character buffer (char[]) of size 10.
Reads 5 characters into buffer, starting at index 2.
Prints the full buffer, showing that the first two positions are empty.
Example - Reading a File in Chunks Using BufferedReader
The following example shows the usage of Java InputStreamReader read(char[] cbuf, int offset, int length) method.
InputStreamReaderDemo.java
package com.tutorialspoint; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class InputStreamReaderDemo { public static void main(String[] args) { try (InputStreamReader reader = new InputStreamReader(new FileInputStream("example.txt")); BufferedReader bufferedReader = new BufferedReader(reader)) { char[] buffer = new char[10]; // Buffer of size 10 int charsRead; while ((charsRead = bufferedReader.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)); // Print full 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 BufferedReader for efficient reading.
Reads 4 characters at a time into buffer, starting at index 3.
Prints the buffer contents after each read operation.