Java PushbackInputStream and PushbackReader Example

By Arvind Rai, November 18, 2023
On this page, we will learn to use java.io.PushbackInputStream and java.io.PushbackReader.

PushbackInputStream

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 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







©2024 concretepage.com | Privacy Policy | Contact Us