English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented(I)

Java Object-Oriented(II)

Java Object-Oriented(III)

Java Exception Handling

Java List(List)

Java Queue(Queue)

Java Map Collection

Java Set Collection

Java Input Output(I/O)

Java Reader/Writer

Java Other Topics

Java BufferedInputStream Class

In this tutorial, we will learn Java BufferedInputStream and its methods with the help of examples.

The BufferedInputStream class in the java.io package is used together with other input streams to read data more efficiently (in bytes).

It inherits the InputStream abstract class.

The working principle of BufferedInputStream

BufferedInputStream maintains a8192bytesinternal buffer.

During the reading operation in BufferedInputStream, a portion of bytes is read from the disk and stored in the internal buffer. Bytes are read one by one from the internal buffer.

This reduces the number of communications with the disk. This is why using BufferedInputStream to read bytes is faster.

Create a BufferedInputStream

To create a BufferedInputStream, we must first import the java.io.BufferedInputStream package. After importing the package, we can create the input stream here.

//Create a FileInputStream
FileInputStream file = new FileInputStream(String path);
//Create a BufferedInputStream
BufferedInputStream buffer = new BufferInputStream(file);

In the above example, we created a buffer named BufferdInputStream and created a file using a FileInputStream named file.

Here, the default size of the internal buffer is8192Bytes. However, we can also specify the size of the internal buffer.

//Create a BufferedInputStream with a specified internal buffer size
BufferedInputStream buffer = new BufferInputStream(file, int size);

The buffer will help read bytes from the file faster.

Methods of BufferedInputStream

The BufferedInputStream class provides implementations for different methods provided by the InputStream class.

The read() method

  • read() - Read a byte from the input stream

  • read(byte[] arr) - Read bytes from the stream and store them in the specified array

  • read(byte[] arr, int start, int length) - Read a number of bytes equal to length from the stream and store them in the specified array starting from position start

Suppose we have a file namedinput.txtwhich contains the following content.

This is a line of text inside the file.

Let's try to use the BufferedInputStream to read a file.

import java.io.BufferedInputStream;
import java.io.FileInputStream;
class Main {
    public static void main(String[] args) {
        try {
            //Create a FileInputStream
            FileInputStream file = new FileInputStream("input.txt");
            //Create a BufferedInputStream
            BufferedInputStream input = new BufferedInputStream(file);
            //Read the first byte from the file
            int i = input.read();
            while (i != -1) {
                System.out.print((char) i);
                //Read the next byte from the file
                i = input.read();
            }
            input.close();
        }
        catch (Exception e) {
            e.getStackTrace();
        }
    }
}

Output Result

This is a line of text inside the file.

In the above example, we created a buffer input stream named 'buffer' and a FileInputStream. The input stream is linked to the file input.txt.

FileInputStream file = new FileInputStream("input.txt");
BufferedInputStream buffer = new BufferedInputStream(file);

Here, we used the read() method to read a byte array from the internal buffer of the buffer reader.

available() method

To get the number of available bytes in the input stream, we can use the available() method. For example,

import java.io.FileInputStream;
import java.io.BufferedInputStream;
public class Main {
   public static void main(String args[]) {
      try {
        //Assuming the input.txt file contains the following text
        //This is a line of text inside the file.
         FileInputStream file = new FileInputStream("input.txt");
         
         //Create a BufferedInputStream
         BufferedInputStream buffer = new BufferedInputStream(file);
         //Returns the number of available bytes
         System.out.println("Available number of bytes at the beginning: "); + buffer.available());
         //Read bytes from the file
         buffer.read();
         buffer.read();
         buffer.read();
         //Returns the number of available bytes
         System.out.println("Available number of bytes at the end: "); + buffer.available());
         buffer.close();
      }
      catch (Exception e) {
         e.getStackTrace();
      }
   }
}

Output Result

Available number of bytes at the beginning: 39
Available number of bytes at the end: 36

In the above example,

  1. First, we use the available() method to check the number of available bytes in the input stream.

  2. Then, we use the read() method3time(s) from the input stream3bytes.

  3. Now, after reading the bytes, we have checked the available bytes again. This time, the available bytes have decreased by3.

skip() method

To discard and skip a specified number of bytes, you can use the skip() method. For example

import java.io.FileInputStream;
import java.io.BufferedInputStream;
public class Main {
   public static void main(String args[]) {
      try {
        //Assuming the input.txt file contains the following text
        //This is a line of text inside the file.
         FileInputStream file = new FileInputStream("input.txt");
         //Create a BufferedInputStream
         BufferedInputStream buffer = new BufferedInputStream(file);
         //Skip5bytes
         buffer.skip(5);
         System.out.println("Skip ");5bytes after the input stream: ");
         //Read the first byte from the input stream:
         int i = buffer.read();
         while (i != -1) {
            System.out.print((char) i);
            //Read the next byte from the input stream
            i = buffer.read();
         }
         //Close the input stream
         buffer.close();
      }
      catch (Exception e) {
         e.getStackTrace();
      }
   }
}

Output Result

Skip5bytes from the input stream after: is a line of text inside the file.

In the above example, we have used the skip() method to skip5bytes. Therefore, skip the bytes 'T', 'h', 'i', 's', and ' ' from the input stream.

close() Method

To close a buffered input stream, we can use the close() method. Once the close() method is called, we will not be able to read data from the input stream.

Other Methods of BufferedInputStream

MethodContent Description
mark()Mark the position of the data read from the input stream
reset()Return the control to the point in the input stream where a mark was set