read(char[], int, int): This method is available in the java.io.Reader class of Java.
Syntax:
int java.io.Reader.read(char[] cbuf, int off, int len) throws IOException
This method takes three arguments. This method reads characters into a portion of an array.
This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.
Parameters: Three parameters are required for this method.
cbuf: Destination buffer.
off: Offset at which to start storing 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
Approach 1: When no exception
Java
import java.io.IOException;import java.io.Reader;import java.io.StringReader;public class Readerread4 {public static void main(String[] args) throws IOException {Reader reader = new StringReader("HELLO");char cbuf[] = { 'a', 'b', 'c', 'd', 'e' };int off = 0, len = 3;System.out.println(reader.read(cbuf, off, len));reader.close();}}
Output:
3
Approach 2: IOException
Java
package com.Reader;import java.io.IOException;import java.io.Reader;import java.io.StringReader;public class Readerread4 {public static void main(String[] args) throws IOException {Reader reader = new StringReader("HELLO");char cbuf[] = { 'a', 'b', 'c', 'd', 'e' };int off = 0, len = 3;reader.close();System.out.println(reader.read(cbuf, off, len));}}
Output:
Exception in thread "main" java.io.IOException: Stream closed at java.base/java.io.StringReader.ensureOpen(StringReader.java:56) at java.base/java.io.StringReader.read(StringReader.java:91) at com.Reader.Readerread4.main(Readerread4.java:16)
Approach 3: IndexOutOfBoundsException
Java
package com.Reader;import java.io.IOException;import java.io.Reader;import java.io.StringReader;public class Readerread4 {public static void main(String[] args) throws IOException {Reader reader = new StringReader("HELLO");char cbuf[] = { 'a', 'b', 'c', 'd', 'e' };int off = 0, len = 13;System.out.println(reader.read(cbuf, off, len));reader.close();}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.StringReader.read(StringReader.java:94) at com.Reader.Readerread4.main(Readerread4.java:15)
Some other methods of Reader
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.
markSupported(): This method tells whether this stream supports the mark() operation.
Reader.nullReader(): This method returns a new Reader that reads no characters.
read(): This method reads a single character.
read(char[]): This method reads characters into an array.
read(CharBuffer): This method attempts to read characters into the specified character buffer.
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.
skip(long): This method skips characters.
transferTo(Writer): This method reads all characters from this reader and writes the characters to the given writer in the order that they are read.
No comments:
Post a Comment