Reading Data from File using BufferedReader
Start course
1h 46m

In this course, we'll learn about the Java Input/Output subject.

Learning Objectives

  • API and Java I/O
  • OutputStream Class
  • InputStream Class
  • Reader Class
  • Writer Class
  • Serialization, Deserialization, and Non-Serializable objects

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams


  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge

Hi there. In this lesson, we'll talk about another subclass of the Reader abstract class, the BufferedReader class. The BufferedReader class of the package can be used with other readers to read data in characters more efficiently. It extends the abstract class Reader, reads text from a character input stream, buffering character so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified or the default size may be used. The default is large enough for most purposes. The BufferedReader maintains an internal buffer of 8,192 characters. During the read operation in BufferedReader, a chunk of characters is read from the disk and stored in the internal buffer. And from the internal buffer, characters are read individually. Hence, the number of communication to the disk is reduced. 

This is why reading characters is faster using BufferedReader. The usage of it and the methods used are generally the same as Reader and LineNumberReader class. The main difference is that the reading process is faster and more efficient. BufferedReader is usually used in large scale projects. Yes, after this information about BufferedReader, let's move on to Eclipse and make an example about the BufferedReader class. First, I'll create a new class, so I right click on the reader example package and select the new class options. So, the class name can be BufferedReaderExample, and I will check the checkbox for the main method and click the 'Finish' button. In this example, we read the data in the message.txt file which we created in the previous video. First, I'll create an object from the FileReader class. FileReader fileReader = new FileReader. The name of the file will be message .txt. And for possible errors, I will write throws IOException right after the main method. 

Yes, so we will no longer receive any warnings from the compiler. Now we'll create an object from the BufferedReader class. BufferedReader bufferedReader = new BufferedReader. And for the constructor parameter, takes a Reader object. So I'll right the FileReader object. Now I will create a variable. This will be of type String, and we'll use it to store the data in each row. So, I write String data. Now I'll create a do-while loop to read the data in the file. In the do block, first I will assign the readLine method of the BufferedReader class to the data variable. data = BufferedReader.readLine ( ). If the data to be read ends and no line terminator is encountered, then readLine method returns the null value. 

Therefore, we can perform a check with the if statement here if the data is not equal to null. In this case, the read data will be transferred to the data string. Now, let's print the data to the console. S.out.println(data). Now, let's create the while condition. This loop should continue until all characters in the file have been read. We can understand from the null value that returns when all the lines are finished. Therefore, I'll write the statement in the if condition here as well. So, if there is data to read, the loop will continue. If the data to read runs out, then this condition will be false and the loop will terminate.

And lastly, I'll close the streams outside the loop. fileReader.close ( ), bufferedReader.close ( ). Everything is ready. Let's run and see the output. As you can see, we have successfully read the data again. As I mentioned at the beginning of the lesson, the use of this class is very similar to the other Reader class. The only difference is that it reads data faster. That's why we often use the BufferedReader class where big data needs to be read. Yes, let's take a short break here. See you in the next lesson.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics