BufferedInputStream Class in Java

The BufferedInputStream class in Java is part of the java.io package and is used to read data from an input stream with a buffer. It provides a more efficient way of reading data by reducing the number of I/O operations by buffering the input. This class is essential for improving performance when reading large amounts of data from an input stream.

Table of Contents

  1. Introduction
  2. Creating a BufferedInputStream
  3. Reading Data from a BufferedInputStream
  4. Closing the Stream
  5. Complete Example
  6. Conclusion

Introduction

The BufferedInputStream class is a subclass of FilterInputStream and is designed to add buffering to input streams. Buffering can improve the performance of I/O operations by reducing the number of physical reads from the underlying source, such as a file or network connection. The BufferedInputStream reads chunks of data into a buffer, from which the data is then read as needed.

Creating a BufferedInputStream

To create a BufferedInputStream, you need to wrap it around another input stream, such as a FileInputStream. You can also specify the size of the buffer, although a default size is provided if you do not specify one.

Example

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

public class BufferedInputStreamExample {
    public static void main(String[] args) {
        try {
            // Creating BufferedInputStream using FileInputStream
            FileInputStream fis = new FileInputStream("example.txt");
            BufferedInputStream bis = new BufferedInputStream(fis);

            // Creating BufferedInputStream with specified buffer size
            BufferedInputStream bisWithBufferSize = new BufferedInputStream(fis, 8192);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Reading Data from a BufferedInputStream

The BufferedInputStream class provides several methods to read data from the input stream:

  • int read(): Reads a single byte.
  • int read(byte[] b): Reads bytes into an array.
  • int read(byte[] b, int off, int len): Reads up to len bytes into an array starting at offset off.

Example

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

public class BufferedInputStreamReadExample {
    public static void main(String[] args) {
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("example.txt"))) {
            int content;
            while ((content = bis.read()) != -1) {
                System.out.print((char) content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Closing the Stream

It is important to close the BufferedInputStream after completing the file operations to release the system resources associated with the stream. This can be done using the close() method.

Example

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

public class BufferedInputStreamCloseExample {
    public static void main(String[] args) {
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream("example.txt"));
            int content;
            while ((content = bis.read()) != -1) {
                System.out.print((char) content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

Alternatively, you can use the try-with-resources statement, which ensures that the stream is closed automatically.

Example

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

public class BufferedInputStreamTryWithResourcesExample {
    public static void main(String[] args) {
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("example.txt"))) {
            int content;
            while ((content = bis.read()) != -1) {
                System.out.print((char) content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Complete Example

Here is a complete example demonstrating the creation, reading, and closing of a BufferedInputStream.

BufferedInputStreamExample.java

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

public class BufferedInputStreamExample {
    public static void main(String[] args) {
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("example.txt"))) {
            int content;
            while ((content = bis.read()) != -1) {
                System.out.print((char) content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Conclusion

The BufferedInputStream class in Java is used for improving the performance of input operations by buffering the input data. By understanding how to create, read, and close a BufferedInputStream, you can effectively handle file I/O operations involving large amounts of data in your Java applications. Remember to always close the stream after use to ensure that system resources are properly released.

Comments