Sunday, 11 June 2017

Java Tutorial : Java IO (Java PushbackInputStream) ~ foundjava


Click here to watch in Youtube :

Click the below Image to Enlarge
Java Tutorial : Java IO (Java PushbackInputStream) 
Java Tutorial : Java IO (Java PushbackInputStream) 
Java Tutorial : Java IO (Java PushbackInputStream) 
myfile.txt
Hello world

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

public class PushbackInputStreamDemo
{

    public static void main(String[] args) throws IOException
    {
        FileInputStream fileInputStream = null;
        PushbackInputStream pushbackInputStream = null;
        try
        {
            fileInputStream = new FileInputStream("myfile.txt");
            pushbackInputStream = new PushbackInputStream(fileInputStream);

            /*
             * Reads the next byte of data from this input
             * stream.
             */
            int data = pushbackInputStream.read();
            System.out.println((char) data);

            /*
             * Pushes back a byte by copying it to the front
             * of the pushback buffer.
             */
            pushbackInputStream.unread(data);

            data = pushbackInputStream.read();
            System.out.println((char) data);

        }
        finally
        {
            if (pushbackInputStream != null)
            {
                /*
                 * Closing a pushbackInputStream will also
                 * close the FileInputStream instance from
                 * which the PushbackReader is reading.
                 */
                pushbackInputStream.close();
            }
        }

    }

}
Output
H
H
Refer: 
https://docs.oracle.com/javase/8/docs/api/java/io/PushbackInputStream.html
Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment