All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Bufferedreader Class In Java

Last Updated : Mar 11, 2024

Bufferedreader Class In Java

In this article we will show you the solution of bufferedreader class in java, Java's BufferedReader class is used to buffer data read via a character stream, like a file. Comparing to other input streams, it offers a more effective method of reading data.

The JVM-allocated buffer, that occupies a small amount of memory, is where data is stored when read from an input stream by the BufferedReader class.

As a result, the programme uses the actual storage device less frequently, which is generally faster than accessing memory. Now let us talk about the Java bufferedreader class shortly.

Step By Step Guide On Bufferedreader Class In Java :-

Data can be read from a variety of sources, including files, sockets, and pipes, using the BufferedReader class.

It reads information from different sources a single line at a time, therefore it keeps reading information up until it comes across a newline character.

The class offers a number of techniques for receiving data from input stream.

import java.io.*;
public class BufferedReader implements FileReader {
    @Override
    public String read(String fileName) {
        String strFileContents = "";
        try(
            FileInputStream fis = new FileInputStream(fileName);
            BufferedInputStream bis = new BufferedInputStream(fis, 256);
        ){
            byte[] b = new byte[256];
            int bytesRead = 0;
            while ((bytesRead = bis.read(b)) != -1){
                strFileContents += new String(b, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strFileContents;
    }
}
  1. You can see that we have written some straightforward Java code to define the "BufferedReader" class, which implements the "FileReader" interface.
  2. This class's function is to read a file's contents using a buffered method.
  3. A method called "read" that takes a filename as a parameter & returns the contents of the file as a string can be found in the "BufferedReader" class.
  4. A string variable named "strFileContents" is set to an empty string inside the procedure.
  5. The file is then opened implementing a "FileInputStream" and wrapped in a "BufferedInputStream" having a buffer size of 256 bytes using a try-catch block in the procedure.
  6. This makes the input stream more effective by allowing it to read data from the file in parts rather than bytes at a time.
  7. The information retrieved from the input stream is saved in a byte array "b" of size 256 bytes.
  8. The procedure then enters a while loop, which iteratively reads data from the input stream until it reaches the file's end, which is indicated by "-1" value the "read" method returns.
  9. The "read" method is used inside the while loop to read up to 256 bytes of data into the "b" byte array from the buffered input stream.
  10. The number of actual bytes read is tracked by the "bytesRead" variable.
  11. The byte array is then transformed into a string using the "String" class's constructor, which requires three parameters: the byte array's starting index, the total length of substring to be generated, and the byte array itself.
  12. The "+=" operator is used to attach the resulting substring to the "strFileContents" string.
  13. This procedure is continued until the entire file has been read.
  14. If an exception arises while reading a file, the catch block catches it and prints the stack trace to the console.
  15. The procedure then outputs a string that contains the entire contents of the file.

Conclusion :-

As a result, we were able to understand the Java bufferedreader class.

Additionally, we discovered that a unique execution of the "FileReader" interface employs a buffered method to read a file's contents.

The implementation is effective and reliable, addressing potential exceptions that can arise while reading files.

I hope this article on bufferedreader class in java helps you and the steps and mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪