Java PushbackInputStream .unread (int b)

Syntax

PushbackInputStream.unread(int b) has the following syntax.

public void unread(int b)  throws IOException

Example

In the following code shows how to use PushbackInputStream.unread(int b) method.


//w  ww . j  ava 2 s  . co m
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.PushbackInputStream;

public class Main {

   public static void main(String[] args) {
      byte[] arrByte = new byte[1024];
      byte[] byteArray = new byte[]{'j', 'a', 'v', 'a', '2','s','.','c','o','m'};

      InputStream is = new ByteArrayInputStream(byteArray);
      PushbackInputStream pis = new PushbackInputStream(is, 10);
      
      try {
         // read from the buffer one character at a time
         for (int i = 0; i < byteArray.length; i++) {
            // read a char into our array
            arrByte[i] = (byte) pis.read();
            System.out.println((char) arrByte[i]);
         }
         // unread a char
         pis.unread('F');

         // read again from the buffer 
         arrByte[1] = (byte) pis.read();
         System.out.println((char) arrByte[1]);
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

The code above generates the following result.