• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Use Java BufferedInputStream with Examples

August 31, 2023 //  by Talha Malik

The BufferedInputStream in Java has the ability to read the data from the buffer. The buffer input stream is useful to store data. The buffered input stream is very effective in reducing the number of I/O operations that eventually help in increasing the performance.

In this article, we look into the Java BufferedInputStream in particular.

How to Use/Implement Java BufferedInputStream with Examples?

The Java BufferedInputStream is responsible for reading the data from the stream. It makes use of the internal buffer to speed up the performance. The performance is also increased due to less number of operations running which makes the applications run faster.

Let us have a look at the syntax of the input buffer stream.

Syntax

FileInputStream fileip = new FileInputStream("D:\\filename.txt");
BufferedInputStream buffip = new BufferedInputStream(fileip);

Example 1: Reading the File

The read() method allows to read the text available in the file. Here is a coding example that will assist you in understanding how to read data from stream:

import java.io.*;

//Declare class for Buffered input stream
class bufferedstream {
    public static void main(String args[]) {
        try {
            // Create a file along with its location
            FileInputStream fileip = new FileInputStream("D:\\bufferedstream.txt");
            //The built-in function for BufferedInputStream.
            BufferedInputStream buffip = new BufferedInputStream(fileip);
            // Declare an integer.
            int j;
            // When the file reaches the end.
            while ((j = buffip.read()) != -1) {
                System.out.print((char) j);
            }
            // close() method closes the stream.
            buffip.close();
            fileip.close();
            // Exception to be thrown for any further operations on the stream.
        } catch (Exception x) {
            System.out.println(x);
        }
    }
}

In the above code,

  • The location of the file has been entered through the FileInputStream.
  • The BufferedInputStream then calls the respective file name declared in the file stream.
  • The while loop for declaration of the read() method if the file reaches the end while the read() method has been executed.
  • The stream has been closed using the close() method in Java.

Output

The above output contains the text written in the file.

Example 2: Available() Method

The number of bytes available in the text file is obtained using the available() method.

import java.io.*;
//Create a class for available bytes.
class newstream {
    public static void main(String args[]) {
        try {
            //Enter the file name along with its location.
            FileInputStream file = new FileInputStream("D:\\bufferedstream.txt");
            //The buffered input stream
            BufferedInputStream buffer = new BufferedInputStream(file);
            //The number of bytes available at the start of the buffer
            System.out.println("The bytes in the starting are: " + buffer.available());

            buffer.read();
            buffer.read();
            //The number of bytes that are present at the end of the buffer
            System.out.println("The bytes in the end are: " + buffer.available());
            //Close the buffer
            buffer.close();
        }
        //Exception case
        catch (Exception e) {
            e.getStackTrace();
        }
    }
}

The above code:

  • The code is similar to the previous one except for a difference that the read() method has been called two times.
  • This read() method will make the ending bytes decrease two times.
  • The available() method gives the number of bytes at the start and at the end  present in the given file.

Output

 The above output illustrates that:

  • The starting bytes are 85.
  • The ending bytes have been decreased twice that are 83.

This article sums up the implementation of BufferedInputStream in detail.

Conclusion

The Java BufferedInputstream is responsible for reading the data in bytes. It works in such a manner that the bytes are stored in the buffer internally and then these bytes are read individually from the internal buffer. It makes the system performance faster by reading the data from the buffer. This article has demonstrated the working of Java BufferedInputStream along with examples.

Category: Java

Previous Post: « How to Perform Sequential Search in Java
Next Post: How to Iterate through an ArrayList using an Iterator in Java »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Use Optional.ofNullable() Method in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact