read(char[], int, int): This method is available in the java.io.CharArrayReader class of Java.
Syntax:
int java.io.CharArrayReader.read(char[] b, 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.
b: Destination buffer.
off: Offset at which to start storing characters.
len: Maximum number of characters to read.
Returns: The actual number of characters read, 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
Approach 1: When no exception
Java
import java.io.CharArrayReader;import java.io.IOException;public class CharArrayReaderread2 {public static void main(String[] args) throws IOException {char buf[] = { 'a', 'b', 'c', 'd' };CharArrayReader charArrayReader =new CharArrayReader(buf);char cbuf[] = { 'a', 'b' };int off = 0, len = 2;System.out.println(charArrayReader.read(cbuf, off, len));charArrayReader.close();}}
Output:
2
Approach 2: IndexOutOfBoundsException
Java
import java.io.CharArrayReader;import java.io.IOException;public class CharArrayReaderread2 {public static void main(String[] args) throws IOException {char buf[] = { 'a', 'b', 'c', 'd' };CharArrayReader charArrayReader =new CharArrayReader(buf);char cbuf[] = { 'a', 'b' };int off = -1, len = 2;System.out.println(charArrayReader.read(cbuf, off, len));charArrayReader.close();}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.CharArrayReader.read(CharArrayReader.java:126)
No comments:
Post a Comment