read(char[], int, int): This method is available in the java.io.StringReader class of Java.
Syntax:
int java.io.StringReader.read(char[] cbuf, int off, int len) throws IOException
This method takes three arguments. This method reads characters into a portion of an array.
Parameters: Three parameters are required for this method.
cbuf: Destination buffer.
off: Offset at which to start writing characters.
len: Maximum number of characters to read.
Returns: The number of characters reads, or -1 if the end of the stream has been reached.
Throws:
1. IOException - If an I/O error occurs.
2. IndexOutOfBoundsException - If off is negative, or len is negative, or len is greater than cbuf.length - off.
3. NullPointerException - If cbuf is null.
Approach 1: When no exception
Java
import java.io.IOException;import java.io.StringReader;public class StringReaderread2 {public static void main(String[] args) {String s = "HELLO JAVA";StringReader stringReader = new StringReader(s);char cbuf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };int off = 0, len = 4;try {System.out.println(stringReader.read(cbuf, off, len));} catch (IOException e) {System.out.println("IOException occurs");} catch (IndexOutOfBoundsException e) {System.out.println("IndexOutOfBoundsException occurs");} catch (NullPointerException e) {System.out.println("NullPointerException occurs");}}}
Output:
4
Approach 2: IOException
Java
import java.io.IOException;import java.io.StringReader;public class StringReaderread2 {public static void main(String[] args) {String s = "HELLO JAVA";StringReader stringReader = new StringReader(s);char cbuf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };stringReader.close();int off = 0, len = 4;try {System.out.println(stringReader.read(cbuf, off, len));} catch (IOException e) {System.out.println("IOException occurs");} catch (IndexOutOfBoundsException e) {System.out.println("IndexOutOfBoundsException occurs");} catch (NullPointerException e) {System.out.println("NullPointerException occurs");}}}
Output:
IOException occurs
Approach 3: IndexOutOfBoundsException
Java
import java.io.IOException;import java.io.StringReader;public class StringReaderread2 {public static void main(String[] args) {String s = "HELLO JAVA";StringReader stringReader = new StringReader(s);char cbuf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };int off = 0, len = 14;try {System.out.println(stringReader.read(cbuf, off, len));} catch (IOException e) {System.out.println("IOException occurs");} catch (IndexOutOfBoundsException e) {System.out.println("IndexOutOfBoundsException occurs");} catch (NullPointerException e) {System.out.println("NullPointerException occurs");}}}
Output:
IndexOutOfBoundsException occurs
Approach 4: NullPointerException
Java
import java.io.IOException;import java.io.StringReader;public class StringReaderread2 {public static void main(String[] args) {String s = "HELLO JAVA";StringReader stringReader = new StringReader(s);char cbuf[] = null;int off = 0, len = 4;try {System.out.println(stringReader.read(cbuf, off, len));} catch (IOException e) {System.out.println("IOException occurs");} catch (IndexOutOfBoundsException e) {System.out.println("IndexOutOfBoundsException occurs");} catch (NullPointerException e) {System.out.println("NullPointerException occurs");}}}
Output:
NullPointerException occurs
Some other methods of StringReader
close(): This method closes the stream and releases any system resources associated with it.
mark(int): This method marks the present position in the stream. Subsequent calls to reset() will reposition the stream to this point.
markSupported(): This method tells whether this stream supports the mark() operation, which it does.
read(): This method reads a single character.
read(char[], int, int): This method reads characters into a portion of an array.
ready(): This method tells whether this stream is ready to be read.
reset(): This method resets the stream to the most recent mark, or to the beginning of the string if it has never been marked.
skip(long): This method skips the specified number of characters in the stream.
StringReader(String): This method creates a new string reader.
No comments:
Post a Comment