Menu
Topics Index
...
`

BufferedInputStreamDemo - Java Example Program


BufferedInputStreamDemo
class BufferedInputStreamDemo
{
    public static void main(String arg[])
    {
        File f1 = new File("E:JavaPrograms\\FolderOne\\fileOne.txt");
        //LINE A
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f1));
        System.out.println("Available Bytes : " + bis.available()); //LINE B
        System.out.println("MarkSuppoted : " + bis.markSupported()); //LINE C
        
        // reading bytes from BufferedInputStream
        System.out.print((char) bis.read());
        System.out.print((char) bis.read());
        // marking position
        bis.mark(2); // LINE D
        
        // reading bytes from BufferedInputStream
        System.out.print((char) bis.read());
        System.out.println((char) bis.read());
        System.out.println("reset method invoked.");
        bis.reset(); // LINE E
        
        int c = 0;
        while((c = bis.read()) != -1)
            System.out.print((char) c);
        bis.close();
    
    }
}
OUTPUT

Available Bytes : 12
MarkSuppoted : true
MERI
reset method invoked.
RIT CAMPUS

DESCRIPTION

At LINE A we created a BufferedInputStream object.
At LINE B we checking for the available bytes. Available bytes will be zero if the file is empty.
At LINE C we are checking whether the file is markSupported.
At LINE D we are marking and the parameter 2 represents two positions to be remembered after mark postion.
At LINE E we are invoking reset method which in turns resets the bytes to the mark position.

THINGS TO TRY
  • Read more than two bytes before invoking reset method. The reset method will throw IOException. If the IOException is not thrown the method resets the bytes to the mark position. Remember the IOException is not guaranteed.
  • Comment LINE D and invoke reset method to see an IOException.

© meritcampus 2019

All Rights Reserved.

Open In App