BufferedInputStream in Java

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 of BufferedInputStream class

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.

example:


File file= new File("D:/Textbook.txt");
FileInputStream fis= new FileInputStream(file);
BufferedInputStream fis= new BufferedInputStream(fis);

Methods for reading BufferedInputStream class

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

Methods Description
available() This method gives the remaining number of bytes available to read from this input stream.
read() This method reads one byte at a time from this input stream.
read(byte[]) This method reads a whole byte array at a time from this input stream.
close() This method closes this input stream and also frees any resources connected with this input stream.