2 Ways to Parse CSV Files in Java - BufferedReader vs Apache Commons CSV Example (original) (raw)

In last tutorial, you have learned how to parse Excel file in Java and in this Java tutorial, you will learn how to parse CSV file in Java. You can directly parse CSV file in Java without using any third party library, because ultimately its a text file and you can use BufferedReader to read it, but you can also take advantage of good open source library like Apache commons CSV to parse comma separated values. These library makes developer's life easy and provides rich functionality to parse various CSV formats. In real programming world, CSV or comma separated files are used for variety of purpose, including for transporting data from one system to another e.g. FX rates, importing and exporting records from database etc.

In CSV files entries are separated by comma, and it may or may not contain header. There are many ways to parse or read CSV files in Java, and if you need to do it on your project, its better not to reinvent the wheel and choose commons csv, but for learning purpose, it's good to know how to do it without using third party library.

In this tutorial, I am going to show you 2 ways to read CSV files in Java. First way is by using java.io.BufferedReader and split() method from java.lang.String class, and second way is by using Apache Commons CSV library's CSVParser class. Commons CSV is new member in rich Apache commons family and has built in support to read most common CSV formats e.g. RFC 4180, Microsoft Excel, MySQL and TDF.

You can also create a custom format by using the fluent style API of Apache commons CSV. CSVParser is fully functional parser, which can parse a different kinds of CSV files e.g. XLS CSV file, CSV file without header or CSV file with header. All you need to do is to choose different format for CSVParser, which we will learn in this tutorial.

By the way, if you want to learn more about reading/writing files in Java then I suggest to join these online core Java courses or read one of the good Java book like Core Java by Cay S. Horstmann or Java: A Beginner's Guide by Herbert Schildt.

Maven dependency and JAR file required for CSV Parsing

In order to use this library you need to add commons-csv-1.1.jar file into your classpath. If you are using Maven you can also add following dependency in your project file.

org.apache.commons commons-csv 1.1

Remember, its better to use Maven for managing dependency because it will also download any other JAR file on which this library is dependent, known as transitive dependencies. If you add JAR files manually, you make sure to download any dependent JAR.

CSV Parser to read CSV files in Java

Apache Commons CSV reads and writes files in variations of the Comma Separated Value (CSV) format. For example to parse an Excel CSV file, you need to write following code

Reader in = ...; Iterable parser = CSVFormat.EXCEL.parse(in); for (CSVRecord record : parser) { ... }

and to read a normal CSV file with header you need to write :

Reader in = ...; Iterable parser = CSVFormat.DEFAULT.parse(in); for (CSVRecord record : parser) { ... }

Currently Apache commons CSV supports following formats :

It's more functional, and should be used in real world project. On the other hand BufferedReader approach is pretty straight forward. You open a CSV file and start reading it line by line, since each line contains a coma separated String, you need to split them using comma (",") and you will get an array of String containing each column.

Just do whatever you wants to do with them, if you are creating object, as shown in first example, then create them, otherwise you can simply print them like in second example. You can even use new Java 7 and Java 8 feature to read file more efficiently.

How to read CSV File in Java using BufferedReader

Java Program to Parse or Read CSV File in Java

Here is full code example of how to read CSV file in Java. This program contains two examples, first one read CSV file without using third party library and the second one parse file using Apache commons CSV, a new library for parsing CSV files. Make sure you include commons-csv-1.1.jar file in your CLASSPATH to run this program in your PC.

Here is our sample CSV file, which also contains header and has contains countries detail e.g. name of country, its capital and currency.

Our CSV file - countries.txt
NAME,CAPITAL,CURRENCY
India,New Delhi,INR
USA,Washington,USD
England,London,GBP
Japan,Tokyo,JPY

There are two methods in this program readCSV() and parseCSV(), former uses BufferedReader to read CSV file. We also have a class Country to represent each line of file, which basically contains country specific data. In first method we read the file line by line and then split each line on comma to get a String array containing individual fields.

We use this array to create Country object and add them into the List, which is returned by our method. Code of this method is very straight forward and self explanatory, we have ignored the first line because we know its header.

Second method is interesting as it demonstrate how to use apache commons csv library to read csv file. As I said, commons csv supports several csv format directly and we will use CSVFormat.DEFAULT, which also supports header. Here you create an instance of CSVParser by passing it a FileInputStream, which points to your csv file and CSVFormat. This contains several CSVRecord from which you can retrieve individual fields.

import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord;

/**

}

Output: Reading from CSV file using BufferedReader and String Split

Country [name=India, capital=New Delhi, currency=INR] Country [name=USA, capital=Washington, currency=USD] Country [name=England, capital=London, currency=GBP] Country [name=Japan, capital=Tokyo, currency=JPY]

Parsing CSV file using CSVParser of Apache commons CSV India New Delhi INR USA Washington USD England London GBP Japan Tokyo JPY

You can see output of our program matches with content of our CSV file. So both of our approach is working properly.

That's all folks, Enjoy parsing CSV file with Apache commons CSV parser. Another great utility open-source library from Apache. You can also report any issue found while using them. A nice way to support open source projects. I suggest to use this library if you have to process CSV files in your project because its tried and tested and rich in functionality. You can use this library to load CSV data into MySQL database or simply create objects from them.

If you like this tutorial and interested to learn more about parsing and handling files in Java, you can also check out following Java IO tutorials from this blog :