Java CSV Parser With Examples

Welcome to the Java CSV Parser tutorial. CSV files are one of the most widely used format to pass data from one system to another. Since CSV files are supported in Microsoft Excel, it can be easily used by non-techies also.

Java CSV Parser

Unfortunately, we don’t have any in-built Java CSV Parser.

If the CSV file is really simple and don’t have any special characters, then we can use Java Scanner class to parse CSV files but most of the times it’s not the case. Rather than writing complicated logic for parsing, it’s better to use open-source tools we have for parsing and writing CSV files.

There are three open-source APIs for working with CSV.

  1. OpenCSV
  2. Apache Commons CSV
  3. Super CSV

We will look into all these java CSV parsers one by one.

Suppose we have a CSV file as:

employees.csv

and we want to parse it to list of Employee object.

1. OpenCSV

We will see how we can use OpenCSV java parser to read CSV file to java object and then write CSV from java object. Download OpenCSV libraries from SourceForge Website and include it in the classpath.

If you are using Maven then include it with below dependency.

For parsing CSV file we can use CSVReader to parse each row to the list of objects. CSVParser also provides an option to read all the data at once and then parse it.

OpenCSV provides CsvToBean class that we can use with HeaderColumnNameMappingStrategy object to automatically map the CSV to list of objects.

For writing CSV data, we need to create List of String array and then use CSVWriter class to write it to the file or any other writer object.

When we run above OpenCSV example program, we get the following output.

As you can see that we can set the delimiters character also while parsing or writing CSV data in OpenCSV java parser.

2. Apache Commmons CSV

You can download the Apache Commons CSV binaries or include the dependencies using maven as shown below.

Apache Commons CSV parser is simple to use and CSVParser class is used to parse the CSV data and CSVPrinter is used to write the data.

Example code to parse above CSV file to the list of Employee objects is given below.

When we run the above program, we get the following output.

3. Super CSV

While searching for good CSV parsers, I saw so many developers recommending Super CSV in Stack Overflow. So I thought to give it a try. Download Super CSV libraries from SourceForge Website and include the jar file in the project build path.

If you are using Maven, just add below dependency.

For parsing CSV file to list of objects, we need to create instance of CsvBeanReader. We can set cell specific rules using CellProcessor array. We can use it to read directly from CSV file to java bean and vice versa.

If we have to write CSV data, process is similar and we have to use CsvBeanWriter class.

When we run above Super CSV example program, we get below output.

As you can see that the Role field is set as Optional because for the third row, it’s empty. Now if we change that to NotNull, we get following exception.

So SuperCSV provides us option to have conditional logic for the fields that are not available with other CSV parsers. It’s easy to use and the learning curve is also very small.

That’s all for the Java CSV parser example tutorial. Whether to use OpenCSV, Apache Commons CSV or Super CSV depends on your requirement and they all seem to be easy to use.

By admin

Leave a Reply

%d bloggers like this: