How to use PushbackInputStream to read back and forth - Java File Path IO

Java examples for File Path IO:File Stream

Description

How to use PushbackInputStream to read back and forth

Demo Code


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;

public class Main {
  public static void main(String[] args) {
    String strExpression = "a = a++ + b;";

    byte bytes[] = strExpression.getBytes();

    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);

    PushbackInputStream pis = new PushbackInputStream(bis);

    int ch;/* ww w .  jav a  2 s .  c o  m*/

    try {

      while ((ch = pis.read()) != -1) {
        if (ch == '+') {
          if ((ch = pis.read()) == '+') {
            System.out.print("Plus Plus");
          } else {
            pis.unread(ch);

            System.out.print("+");
          }
        } else {
          System.out.print((char) ch);
        }
      }
    } catch (IOException ioe) {
      System.out.println("Exception while reading" + ioe);
    }
  }
}

Result


Related Tutorials