Java PushbackInputStream read and unread

Description

Java PushbackInputStream read and unread

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;

public class Main {
  public static void main(String[] args) {
    String srcFile = "Main.java";

    try (PushbackInputStream pis = new PushbackInputStream(new FileInputStream(
        srcFile))) {// w ww.  j a va  2  s .  c  o  m
      // Read one byte at a time and display it
      int data;
      while ((data = pis.read()) != -1) {
        System.out.print((char) data);
        // Unread the last byte that we have just read
        pis.unread(data);

        // Reread the byte we pushed back
        data = pis.read();
        System.out.print((char) data);
      }
    } catch (IOException e2) {
      e2.printStackTrace();
    }
  }
}



PreviousNext

Related