Search This Blog

Monday, June 9, 2025

Java PushbackInputStream - Code Example

What will be the output of the following PushbackInputStream code:

You have a text file Test1.txt. The test1.txt contains the following text: 

abcdefghijklmnopqrstuvwxyz

The following code reads the file Test1.txt. Can you guess the output of the following code?

/**
 *
 * @author Nancy K A N
 */
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PushbackInputStream;

public class PushbackInputStreamDemo {
    public static void main(String[] args) throws IOException{
        try {
            
            PushbackInputStream piStream= new PushbackInputStream(
                    new FileInputStream("C:\\JavaPractice\\BlogJava\\Test1.txt"));
            int readByte = piStream.read();
            System.out.println((char)(readByte));
            readByte=piStream.read();
            System.out.println((char)(readByte));
            readByte=piStream.read();
            System.out.println((char)(readByte));
            piStream.unread(readByte);
            readByte=piStream.read();
            System.out.println((char)(readByte));
           
        } catch (FileNotFoundException ex) {
            System.out.println("File not found");
        }       
    }
}



Created by Nancy N.
@14:50 09-Jun-2025



















No comments:

Post a Comment