read(char[], int, int): This method is available in the java.io.PushbackReader class of Java.
Syntax:
int java.io.PushbackReader.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
Approach 1: When no exception
Java
import java.io.IOException;import java.io.PushbackReader;import java.io.Reader;import java.io.StringReader;public class PushbackReaderread2 {public static void main(String[] args) throws IOException {Reader in = new StringReader("HELLO");PushbackReader pushbackReader = new PushbackReader(in);char cbuf[] = { 'a', 'b', 'c', 'd', 'e' };int off = 0, len = 3;System.out.println(pushbackReader.read(cbuf, off, len));pushbackReader.close();}}
Output:
3
Approach 2: IOException
Java
package com.PushbackReader;import java.io.IOException;import java.io.PipedReader;import java.io.PushbackReader;import java.io.Reader;public class PushbackReaderread2 {public static void main(String[] args) throws IOException {Reader in = new PipedReader();PushbackReader pushbackReader = new PushbackReader(in);char cbuf[] = { 'a', 'b', 'c', 'd', 'e' };int off = 0, len = 3;System.out.println(pushbackReader.read(cbuf, off, len));pushbackReader.close();}}
Output:
Exception in thread "main" java.io.IOException: Pipe not connected at java.base/java.io.PipedReader.read(PipedReader.java:293) at java.base/java.io.FilterReader.read(FilterReader.java:75) at java.base/java.io.PushbackReader.read(PushbackReader.java:129) at com.PushbackReader.PushbackReaderread2.main(PushbackReaderread2.java:17)
Approach 3: IndexOutOfBoundsException
Java
package com.PushbackReader;import java.io.IOException;import java.io.PushbackReader;import java.io.Reader;import java.io.StringReader;public class PushbackReaderread2 {public static void main(String[] args) throws IOException {Reader in = new StringReader("HELLO");PushbackReader pushbackReader = new PushbackReader(in);char cbuf[] = { 'a', 'b', 'c', 'd', 'e' };int off = 0, len = 13;System.out.println(pushbackReader.read(cbuf, off, len));pushbackReader.close();}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.StringReader.read(StringReader.java:94) at java.base/java.io.FilterReader.read(FilterReader.java:75) at java.base/java.io.PushbackReader.read(PushbackReader.java:129) at com.PushbackReader.PushbackReaderread2.main(PushbackReaderread2.java:17)
Some other methods of PushbackReader
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, which it does not.
PushbackReader(Reader): This method creates a new pushback reader with a one-character pushback buffer.
PushbackReader(Reader, int): This method creates a new pushback reader with a pushback buffer of the given size.
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.
skip(long): This method skips characters
unread(char[]): This method pushes back an array of characters by copying it to the front of the pushback buffer.
unread(int): This method pushes back a single character by copying it to the front of the pushback buffer.
unread(char[], int, int): This method pushes back a portion of an array of characters by copying it to the front of the pushback buffer.
No comments:
Post a Comment