< Prev
Next >


File Input/Output operations consume a lot of important resources and are time consuming. Hence, reading a chunk of bytes out of a file and storing it in a local buffer for later processing is faster and than reading a byte at a time, out of a file. Such buffering is used to speed up the I/O process.

BufferedInputStream class is used to create such buffered input stream through which a chunk of bytes are read out of a file for later processing.

Constructors :

BufferedInputStream(InputStream is)
This constructor creates a BufferedInputStream object to read the data out of a file, which accessed by an InputStream, passed to this constructor as a parameter.

File file= new File("D:/Textbook.txt");
FileInputStream fis= new FileInputStream(file);
BufferedInputStream fis= new BufferedInputStream(fis);
This example has created a BufferedInputStream object to read the data out of a file "D:/Textbook.txt" through InputStream i.e FileInputStream, passed to constructor. As soon as this constructor is called, a large chunk of bytes is read out of file and stored in the local buffer of BufferedInputStream.

Point to remember

FileInputStream is a subclass of InputStream

Methods for reading BufferedInputStream

Some methods that we generally use while working with BufferedInputStream are shown in the table below :

Methods Description
This method gives the remaining number of bytes available to read from this input stream.

This method reads one byte at a time from this input stream.

This method reads a whole byte array at a time from this input stream.

This method closes this input stream and also frees any resources connected with this input stream.

Program to read one byte at a time out of an existing file.

Using BufferedInputStream to read an existing file called "TextBook.txt" through its path D:\\TextBook.txt. Let's say the contents of this file TextBook.txt are -
Hello there. How are you doing? 
//Program to create a BufferedInputStream to read(one byte at a time) out of an existing file.

import java.io.*;
class A
public static void main(String... ar)
FileInputStream fis= new FileInputStream("D:/Textbook.txt");
BufferedInputStream bis= new BufferedInputStream(fis); //A local buffer is created which is big-
							//enough to hold chunk of bytes read out of file TextBook.txt

System.out.println("Bytes available to read = "+ bis.available());

int c;
while( (c=bis.read())!=-1) //reading local buffer(one byte at a time) until EOF is reached

catch(IOException e)


Output is -:

Bytes available to read = 31
Hello there. How are you doing?

Program Analysis

Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter

< Prev
Next >
< ByteArrayOutputStream
BufferedOutputStream >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next