Java.io.PushbackReader Class in Java
java.io.PushbackReader is a character-stream reader class that allows characters to be pushed back into the Stream.
Declaration:
public class PushbackReader extends FilterReader
Constructors :
- PushbackReader(Reader push) : Creates a new Pushback Reader – “push” with a character Pushback buffer.
- PushbackReader(Reader push, int size) : Creates a new Pushback Reader with a Pushback buffer of a particular size.
Methods of PushbackReader class :
- read() : java.io.PushbackReader.read() reads a single character.
Syntax :public int read() Parameters : ----------- Return : reads single character from the Pushback buffer else, -1 i.e. when end of file is reached. Exception : -> IOException : If I/O error occurs.
- read(char[] carray, int offset, int maxlen) : java.io.PushbackReader.read(char[] carray, int offset, int maxlen) reads some portion of the character array
Syntax :public int read(char[] carray, int offset, int maxlen) Parameters : carray : destination buffer to be read into offset : starting position of the carray maxlen : maximum no. of characters to be read Return : reads some portion of the character array else, -1 i.e. when end of file is reached. Exception : -> IOException : If I/O error occurs.
- close() : java.io.PushbackReader.close() closes the PushbackReader and releases system resources associated with this stream to Garbage Collector.
Syntax :public void close() Parameters : ------ Return : void Exception : -> IOException : If I/O error occurs.
- mark(int arg) : java.io.PushbackReader.mark(int arg) marks the current position of the PushbackReader. In case of PushbackReader, this method always throws an exception.
Syntax :public void mark(int arg) Parameters : arg : integer specifying the read limit of the Stream Return : void
- skip(long char) : java.io.PushbackReader.skip(long char) skips and discards ‘char’ no. of characters from PushbackReader data.
Syntax :public long skip(long char) Parameters : char : no. of bytes of PushbackReader data to skip. Return : no. of bytes to be skipped Exception: -> IOException : in case I/O error occurs
- reset() : java.io.PushbackReader.reset() is invoked by mark() method. It repositions the PushbackReader to the marked position. In case of PushbackReader, this method always throws an exception.
Syntax :public void reset() Parameters : ---- Return : void Exception : -> IOException : If I/O error occurs.
- markSupported() : java.io.PushbackReader.markSupported() Tells whether this stream supports the mark() operation, which it does not.
Syntax :public boolean markSupported() Parameters : ------- Return : true if PushbackReader supports the mark() method else,false
- ready() : java.io.PushbackReader.ready() tells whether the stream is ready to be read or not
Syntax :public boolean ready() Parameters : ------- Return : true if the stream is ready to be read else,false
- unread(int char) : java.io.PushbackReader.unread(int char) pushes a single character to Pushback buffer. The next character to be read will have (char)char value, after this method returns.
Syntax :
public void unread(int char) Parameters : char : int value of the character to be pushed back Return : void Exception : -> IOException : If I/O error occurs or Pushback buffer is full.
- unread(char[] carray) : java.io.PushbackReader.unread(char[] carray) pushes an array of character to Pushback buffer. The array being pushed will have its index starting from 0.
Syntax :public void unread(char[] carray) Parameters : carray : character array to be pushed back Return : void Exception : -> IOException : If I/O error occurs or Pushback buffer is full.
Java code explaining the PushbackReader method : read(char[] carray), close(), markSupported(), read(), mark(), ready(), skip(), unread()
// Java program illustrating the working of PushbackReader // read(char[] carray), close(), markSupported() // read(), mark(), ready(), skip(), unread() import java.io.*; public class NewClass { public static void main(String[] args) throws IOException { try { // Initializing a StringReader and PushbackReader String s = "w3wiki" ; StringReader str_reader = new StringReader(s); PushbackReader geek_pushReader1 = new PushbackReader(str_reader); PushbackReader geek_pushReader2 = new PushbackReader(str_reader); // Use of ready() method : System.out.println( "Is stream1 ready : " + geek_pushReader1.ready()); System.out.println( "Is stream2 ready : " + geek_pushReader2.ready()); // Use of read() : System.out.println( "\nWe have used skip() method in 1 : " ); System.out.print( "\nUse of read() in 1 : " ); for ( int i = 0 ; i < 6 ; i++) { char c = ( char ) geek_pushReader1.read(); System.out.print(c); // Use of skip() : geek_pushReader1.skip( 1 ); } System.out.println( "" ); // USing read() : char [] carray = new char [ 20 ]; System.out.println( "Using read() in 2 : " + geek_pushReader2.read(carray)); // USe of markSupported() : System.out.println( "\nIs mark supported in 1 : " + geek_pushReader1.markSupported()); geek_pushReader2.unread( 'F' ); // read the next char, which is the one we unread char c3 = ( char ) geek_pushReader2.read(); System.out.println( "USe of unread() : " + c3); // USe of mark() : geek_pushReader1.mark( 5 ); // Use of close() : geek_pushReader1.close(); } catch (IOException excpt) { System.out.println( "mark not supported in 1" ); } } } |
Output :
Is stream1 ready : true Is stream2 ready : true We have used skip() method in 1 : Use of read() in 1 : GesoGe Using read() in 2 : 1 Is mark supported in 1 : false USe of unread() : F mark not supported in 1
Contact Us