The Wayback Machine - https://web.archive.org/web/20220614070115/https://www.geeksforgeeks.org/java-io-bufferedinputstream-class-java/amp/

Java.io.BufferedInputStream class in Java

A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time.

Constructor and Description

Methods:

Program:






// Java program to demonstrate working of BufferedInputStream
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
  
// Java program to demonstrate BufferedInputStream methods
class BufferedInputStreamDemo
{
    public static void main(String args[]) throws IOException
    {
        // attach the file to FileInputStream
        FileInputStream fin = new FileInputStream("file1.txt");
  
        BufferedInputStream bin = new BufferedInputStream(fin);
  
        // illustrating available method
        System.out.println("Number of remaining bytes:" +
                                            bin.available());
  
        // illustrating markSupported() and mark() method
        boolean b=bin.markSupported();
        if (b)
            bin.mark(bin.available());
  
        // illustrating skip method
        /*Original File content:
        * This is my first line
        * This is my second line*/
        bin.skip(4);
        System.out.println("FileContents :");
  
        // read characters from FileInputStream and
        // write them
        int ch;
        while ((ch=bin.read()) != -1)
            System.out.print((char)ch);
  
        // illustrating reset() method
        bin.reset();
        while ((ch=bin.read()) != -1)
            System.out.print((char)ch);
  
        // close the file
        fin.close();
    }
}

Output:

Number of remaining bytes:47
FileContents :
 is my first line
This is my second line
This is my first line
This is my second line


Next Article:
Java.io.BufferedOutputStream class in Java

This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Article Tags :
Practice Tags :