Java

Java BufferedReader

Java’s BufferedReader class uses character buffers to ensure an efficiency when reading text from a character input stream. Typically, each read query made to a Reader triggers a similar read request to be sent to the character or byte stream underneath. Therefore, it is advisable to use a BufferedReader to enclose any Reader that might have expensive read operations, which include FileReaders and InputStreamReaders. It helps to facilitate performance. It is descended from the Reader class.

Example # 1: Using a BufferedReader With a Stream in Java in Ubuntu 20.04

An input stream can be used as the source for a BufferedReader. To read data from System.in, let’s encapsulate the InputStreamReader. The information we type on the keyboard will be read by it.

To the java program above, we have imported the package “java.io.BufferedReader” along with other java packages. Next, we have created the public java class as “Test1” and built the main method inside it that also throws IOExecption if anything goes wrong within the main method. Here, we declared the variable “myStream” from the class “InputStreamReader” and assigned the “system.in” that allows us to read the input stream from the user. Then, we create the BufferedReader in the variable “myBufferedReader” and passed “myStream” as a parameter. The “line” variable read the input stream as we have set the readLine method with the “myBufferedReader”. We have displayed the input stream from the keyword through the system.out.print method and closed the BufferedReader.

The BufferedReader reads the input stream in the following java console.

Example # 2: Using a BufferedReader to Read Files Line by Line in Java in Ubuntu 20.04

The BufferedReader class offers many methods for reading data. One line is read at a time by the readLine() function. This method yields null if the stream terminates. To read a file, let’s employ this method:

To the java program main, we have given the file path “File.txt” which is read by the FileReader class. The BufferedReader read the data from this file which has a few lines of text. For this, we have created the variable “bufferedReader” and invoked the BufferedReader which takes the “fileReader” as an argument. We have employed the while loop, which cycled over each line in the given file and read by the BufferedReader method called readLine(). In the end, BufferedReader is closed.

You can see the BufferedReader method readLine() read the file content line by line and display it in the output.

Example # 3: Using a BufferedReader to Read a Character From a File in Java in Ubuntu 20.04

The BufferedReader class’s read() method allows us to read only one character at a time. The char read is returned by this procedure as an integer. If we approach the stream’s end, the function returns -1. The operation of this technique is shown in the code below. We have the line “This is a text file” in our file.

The java program is quite the same as the aforementioned illustration. Here, we have provided the file name “File.txt” to the variable “Filename”. The “Reader” reads the file and then, the “Reader” is passed to the BufferedReader. The BufferedReader called the read() method inside the while loop and returned the character one by one in the file.

The characters from the file are fetched one by one by the read() method of the java BufferedReader.

Example # 4: Using a BufferedReader to Read Multiple Characters in Java in Ubuntu 20.04

Additionally, many characters can be read simultaneously using the read() method. A char array that will hold the data must be supplied. Additionally, we must specify an offset to denote the char array’s beginning index. This index serves as the starting point for the saved data. It is also necessary to state how many characters can be read simultaneously.

First, we have given the file name and then create the BufferedReader. After this, we have established the char[] array which takes six characters at a time. Inside the “file.txt”, we have the input phrase “text”. The read() function of BufferedReader will be used to acquire all of the file’s characters. To the read() method, we have passed the starting index and ending index of the character array so the read() method read six characters from the file.

As we have a “text” word inside our file, the read() method outputs all the characters at once from the file.

Example # 5: Using a BufferedReader for Skipping Characters in Java in Ubuntu 20.04

A skip() function is available in the BufferedReader class, which can be used to exclude characters from the text. The type parameter is quite long. We have the line “My text file” in our file. We must skip one character after reading each one.

We have created the BufferedReader and passed the fileReader variable to it, which has the file path. Then, we defined the “chRead” object. We have also defined the StringBuilder method for a mutable sequence of the characters in the variable “str_b”. We have utilized the while loop which is called the read() method that read the character at a time. As the characters are read one by one then, we have the skipped method which skips the next character after reading the one character.

After the character is skipped from the provided line, the output is obtained like this:

Example # 6: Using a BufferedReader for Mark and Rest in Java in Ubuntu 20.04

For marking a specific character, the BufferedReader class has the mark() function. Whenever we want in the future, we can use the reset() method to return to the designated character. An integer that represents the number of bytes that can be read in total before the mark is considered incorrect is the input for the mark() method.

We have the word “Li*nux” present in our file. We have specified the file “file.txt” to the fileReader. Then, we constructed the BufferedReader where the fileReader is passed. All the characters will be read, but we will only read the ones with an asterisk. After that, we’ll use reset to return to this point(). For this, we have defined the array with the index specified for each character. After the two indexes of the array, we marked the asterisk y mark method and skipped it by the skip method. The rest method will take back to the marked character at any time.

The asterisk marked in the file is skipped by the above implementation of the code.

Conclusion

The BufferedReader class is used to read data from sources like files or streams, using a buffer that minimizes I/O operations, enhances the performance of other readers. Many practical techniques to read data are available through the BufferedReader class. An individual line can be read using the readLine() function. You can read one or more characters using the read() method.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.