Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Io Java IO PushbackReader

Related Tutorials


 
 

Share on Google+Share on Google+

Java IO PushbackReader

Advertisement
In this section we will discuss about the PushbackReader class in Java.

Java IO PushbackReader

In this section we will discuss about the PushbackReader class in Java.

PushbackReader is a class of java.io package that extends the java.io.FilterReader, reads the character streams. PushbackReader class provides the characters that they can be pushed back into the stream.

Constructor of PushbackReader

Constructor
Description
PushbackReader(Reader in) This constructor is used to create a new PushbackReader which contains the object of Reader (input stream).
Syntax : public PushbackReader(Reader in)
PushbackReader(Reader in, int size) This constructor is used to create a new PushbackReader which contains the object of Reader (input stream) as well as defined the specified size of buffer.
Syntax : public PushbackReader(Reader in, int size)

Methods detail

  • close() : This method is used to close the stream as well as to release the resources associated with the stream.

    Syntax : public void close() throws IOException
     
  • mark(int readAheadLimit) : This method is used to mark the current position in the stream.

    Syntax : public void mark(int readAheadLimit) throws IOException
     
  • markSupported() : This method returns a boolean value which specifies whether this stream supports the mark() method or not.

    Syntax : public boolean markSupported()
     
  • read() : This method is used to read a character from the stream.

    Syntax : public int read() throws IOException
     
  • read(char[] cbuf, int off, int len) : This method is used to read the specified length of characters from the specified char array buffer starts at the specified position.

    Syntax : public int read(char[] cbuf, int off, int len) throws IOException
     
  • ready() : This method returns a boolean value which specifies whether this stream is ready to be read or not.

    Syntax : public boolean ready() throws IOException
     
  • reset() : This method is used to read reset the current position marked with the mark() method.

    Syntax : public void reset() throws IOException
     
  • skip(long n) : This method is used to skip the specified number of characters.

    Syntax : public long skip(long n) throws IOException
     
  • unread(char[] cbuf) : This method is used to push back the character's array. To push back an array of characters it copied the characters to the front of the pushback buffer.

    Syntax : public void unread(char[] cbuf) throws IOException
     
  • unread(char[] cbuf, int off, int len) : This method is used to push back the specified length of character's array. To push back an array of characters it copied the characters to the front of the pushback buffer.

    Syntax : public void unread(char[] cbuf, int off, int len) throws IOException
     
  • unread(int c) : This method is used to push back a character. To push back a character it copied the characters to the front of the pushback buffer.

    Syntax : public void unread(int c) throws IOException

Example

Example of PushbackReader class is being given here which will demonstrate you how to use this class. In this example I have created a simple Java class named JavaPushbackReaderExample.java. In this example you will see that java.io.FileReader class is used this class reads the data of a file as character. Further I have used PushbackReader class and passed the object of FileReader (since FileReader extends the Reader so FileReader is a Reader) which will be treated as data source for the PushbackReader. Then read the data using the read() method.

Source Code

JavaPushbackReaderExample.java

import java.io.PushbackReader;
import java.io.FileReader;
import java.io.IOException;

public class JavaPushbackReaderExample
{
   public static void main(String args[])
     {
        FileReader fr = null;
        PushbackReader pr = null;
        try
          {
             fr = new FileReader("test.txt");
             pr = new PushbackReader(fr);
             int d;
             while((d = pr.read()) != -1)
               {
                  System.out.print((char)d);	                      
               }              
          }
        catch(IOException ioe)
          {
             System.out.println(ioe);
          }
        finally
          {
             if(fr != null)
              {
                 try
                   {
                      fr.close();
                   }
                 catch(Exception e)
                   {
                      System.out.println(e);
                   }
              }
             if(pr != null)
              {
                 try
                   {
                      pr.close();
                   }
                 catch(Exception e)
                   {
                      System.out.println(e);
                   }
              }
          }
     }
}

Output

When you will execute the above code you will get the output as follows :

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 20, 2012

Related Tutorials

Discuss: Java IO PushbackReader  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com