Home  >  Core Java

Java PushbackInputStream and PushbackReader Example

By Arvind Rai, July 19, 2014
Java.io.PushbackInputStream belongs to JDK 1.0 and java.io.PushbackReader belongs to the JDK 1.1. In this page I will provide the examples of both API.

PushbackInputStream

PushbackInputStream overrides InputStream and provides extra functionality. PushbackInputStream can push back one byte. It means PushbackInputStream can unread a byte which is already read. PushbackInputStream is useful when there is delimiter in the input data and we need to split the data on the basis of that delimeter. So read the delimiter and then push back one byte.
PushbackInputStreamDemo.java
package com.cp.io;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;
public class PushbackInputStreamDemo {
	  public static void main(String[] args) {
          String strExp = "a--b-cb---ab";
          byte bytes[] = strExp.getBytes();
          ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
          PushbackInputStream pbis = new PushbackInputStream(bais);
          int c;
          try {
              while( (c = pbis.read())!= -1) {
                  if(c == '-') {
              	      int nextC;
                      if( (nextC = pbis.read()) == '-'){
                           System.out.print("**");
                      }else {
                    	  //push backs one byte
                       	  pbis.unread(nextC);
                          System.out.print((char)c);
                      }
	              }else {
	                          System.out.print((char)c);
	              }
             }
          } catch(IOException ioe) {
                  System.out.println("Exception while reading" + ioe);
          }
  }
} 
Output will as below.
a**b-cb**-ab

PushbackReader

PushbackReader is a character stream reader. PushbackReader pushes back a character into stream. PushbackReader is overrides FilterReader.
PushbackReaderDemo.java
package com.cp.io;
import java.io.CharArrayReader;
import java.io.IOException;
import java.io.PushbackReader;
public class PushbackReaderDemo {
	public static void main(String[] args) {
        char charArray[] = {'a','-','-','b','-','c','b','-','-','-','a','b'};
        CharArrayReader chARed = new CharArrayReader(charArray); 
        PushbackReader pbr = new PushbackReader(chARed);
        int c;
        try {
            while( (c = pbr.read())!= -1) {
                if(c == '-') {
            	    int nextC;
                    if( (nextC = pbr.read()) == '-'){
                         System.out.print("**");
                    }else {
                  	  //push backs a single character
                    	pbr.unread(nextC);
                        System.out.print((char)c);
                    }
	            }else {
	                    System.out.print((char)c);
	            }
           }
        } catch(IOException ioe) {
                System.out.println("Exception while reading" + ioe);
        }
    }
} 
Output will as below.
a**b-cb**-ab
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us