Java.io.StringReader.read() Method
Advertisements
Description
The java.io.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() 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
The following example shows the usage of java.io.StringReader.read() method.
package com.tutorialspoint;
import java.io.*;
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();
}
}
}
Let us compile and run the above program, this will produce the following result:
Hello