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

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

It extends the InputStream abstract class.


Working of BufferedInputStream

The BufferedInputStream keeps up an inward buffer of 8192 bytes.

During the reading activity in BufferedInputStream, a chunk of bytes is read from the disk and stored in the inner buffer. What’s more, from the inward buffer bytes are read exclusively.

Subsequently, the quantity of correspondence to the disk is decreased. This is the reason perusing bytes is quicker using the BufferedInputStream.


Make a BufferedInputStream

To make a BufferedInputStream, we should import the java.io.BufferedInputStream package first. When we import the package here is the manner by which we can make the input stream.

// Creates a FileInputStream
FileInputStream file = new FileInputStream(String path);

// Creates a BufferedInputStream
BufferedInputStream buffer = new BufferInputStream(file);

In the above example, we have made a BufferdInputStream named buffer with the FileInputStream named file.

Here, the inner buffer has the default size of 8192 bytes. However, we can indicate the size of the inner buffer also.

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

The buffer will help to read bytes from the files more quickly.


Methods of BufferedInputStream

The BufferedInputStream class provides implementations for different methods present in the InputStream class.

read() Method

  • read() – reads a single byte from the input stream
  • read(byte[] arr) – reads bytes from the stream and stores in the specified array
  • read(byte[] arr, int start, int length) – reads the number of bytes equal to the length from the stream and stores in the specified array starting from the position start

Suppose we have a file named input.txt with the following content.

This is a line of text inside the file.

Let’s try to read the file using BufferedInputStream.

import java.io.BufferedInputStream;
import java.io.FileInputStream;

class Main {
    public static void main(String[] args) {
        try {

            // Creates a FileInputStream
            FileInputStream file = new FileInputStream("input.txt");

            // Creates a BufferedInputStream
            BufferedInputStream input = new BufferedInputStream(file);

            // Reads first byte from file
            int i = input .read();

            while (i != -1) {
                System.out.print((char) i);

                // Reads next byte from the file
                i = input.read();
            }
            input.close();
        }

        catch (Exception e) {
            e.getStackTrace();
        }
    }
}

Output

This is a line of text inside the file.

In the above example, we have made a buffered input stream named buffer alongside FileInputStream. The input stream is connected with the record input.txt.

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

Here, we have used the read() method to read an array of bytes from the internal buffer of the buffered reader.


Available() Method

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

import java.io.FileInputStream;
import java.io.BufferedInputStream;

public class Main {

   public static void main(String args[]) {

      try {

         // Suppose, the input.txt file contains the following text
         // This is a line of text inside the file.
         FileInputStream file = new FileInputStream("input.txt");
         
         // Creates a BufferedInputStream
         BufferedInputStream buffer = new BufferedInputStream(file);

         // Returns the available number of bytes
         System.out.println("Available bytes at the beginning: " + buffer.available());

         // Reads bytes from the file
         buffer.read();
         buffer.read();
         buffer.read();

         // Returns the available number of bytes
         System.out.println("Available bytes at the end: " + buffer.available());

         buffer.close();
      }

      catch (Exception e) {
         e.getStackTrace();
      }
   }
}

Output

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

In the above example,

  • We first use the available() method to check the number of available bytes in the input stream.
  • Then, we have used the read() method 3 times to read 3 bytes from the input stream.
  • Now, after reading the bytes we again have checked the available bytes. This time the available bytes decreased by 3.

skip() Method

To dispose of and avoid the predetermined number of bytes, we can use the skip() technique. For instance,

import java.io.FileInputStream;
import java.io.BufferedInputStream;

public class Main {

   public static void main(String args[]) {

      try {
         // Suppose, the input.txt file contains the following text
         // This is a line of text inside the file.
         FileInputStream file = new FileInputStream("input.txt");

         // Creates a BufferedInputStream
         BufferedInputStream buffer = new BufferedInputStream(file);

         // Skips the 5 bytes
         buffer.skip(5);
         System.out.println("Input stream after skipping 5 bytes:");

         // Reads the first byte from input stream
         int i = buffer.read();
         while (i != -1) {
            System.out.print((char) i);

            // Reads next byte from the input stream
            i = buffer.read();
         }

         // Closes the input stream
         buffer.close();
      }

      catch (Exception e) {
         e.getStackTrace();
      }
   }
}

Output

Input stream after skipping 5 bytes: is a line of text inside the file.

In the above example, we have used the skip() strategy to avoid 5 bytes from the record input stream. Henceforth, the bytes ‘T’, ‘h’, ‘I’, ‘s’ and ‘ are skipped from the input stream.


close() Method

To close the buffered input stream, we can use the close() technique. When the close() strategy is called, we can’t use the input stream to read the data.


Other Methods Of BufferedInputStream

MethodsDescriptions
mark()mark the position in input stream up to which data has been read
reset()returns the control to the point in the input stream where the mark was set

To learn more, visit Java BufferdInputStream (official Java documentation).


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.


2 Comments

Sarai Gilgour · December 12, 2020 at 2:56 pm

i like this flawless post

erotik izle · December 14, 2020 at 9:35 pm

You completed certain good points there. I did a search on the subject matter and found nearly all people will have the same opinion with your blog.

Leave a Reply

Avatar placeholder