Javarevisited
Published in

Javarevisited

How To Read Text File With BufferedReader In Java

In this article, we will read CSV file from Kaggle about Netflix's top 10 movies using BufferedReader class

Originally published at https://asyncq.com

Introduction

BufferedReader class is one of the most used when it comes to read Text files in Java.This class provides methods that can read characters from input stream. As name says it buffers read characters hence efficient in reading files. In this article we will read csv file from Kaggle about Netflix top 10 movie using BufferedReader class.

Input File

I am using Netflix daily top 10 datasets from Kaggle as my input file.

BufferedReader

BufferedReader class is provided by Java SDK and we don’t need any additional library. It provides enough methods to read CSV files from local disk.

Infrastructure code

To get BufferedReader Object from CSV file on Disk our code looks like below

File Reading Logic

  • BufferedReader class provides method readLine() to read each line from CSV file.
    readLine() Reads a line of text & line is considered to be terminated by any one of a line feed (‘\n’), a carriage return (‘\r’), or a carriage return followed immediately by a linefeed.
  • Each line would be String and we can split that string using a comma delimiter for CSV. If its tab we can use tab separator as well.
  • Once we have a String array of CSV fields then its good practice to convert that into Java POJO using mapper class. In our case , we have created a MovieMapper class that has logic to map CSV fields to Java objects.

Output

We decided to just console print the validated record, but in real word scenario you would either perform a certain transformation or just insert into some sink such as database.

Client code

  • Our client code creates two list, one for error record and other for valid records. If we encounter error records we should filter it out from our input records and write to separate sink maybe some sort of error file which operation team member can look into later.
  • For validated records we can further process maybe do some grouping operation or some sort of further transformation based on business logic and finally write it down to some Sink. Here i have just output on console.

List of other methods

Read using lines() method
This method returns Stream<String>, suitable for reading csv reacord as a stream of record and use functional-style programming

Read using read() method

This method read file character by character. Since the return value is int we need to parse it to char.

Read using read(cbuff,off,len) method
Read characters into a portion of an array. In below example read character will be stored in readChar char[] array which will start reading from offset and will read until defined length.

Conclusion

Java BufferedReader class provides methods to read text file which is sufficient for many use cases. It provides Stream of Input source as well which can be useful for folks who love functional programming.It is thread-safe as well which means if more than one thread try to access Bufferedreader object it will put lock using synchronized and avoid inconsistent result.

Further Readings

Let’s connect on LinkedIn
If you like this article, please check more @ https://asyncq.com/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suraj Mishra

Suraj Mishra

323 Followers

Google Cloud Certified Professional Data Engineer | Backend Engineering @ Rakuten Card | Blogs @https://asyncq.com/