
- 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 - 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.