Working with CSV files in java using Apache Commons CSV

 

Apache commons CSV library is a java library that can be used to read and write CSV files in a very simple and easy way. Another advantage is that this java library is open source and freely available to use.

Add the library to your project as :

Maven dependency :

You just need to add this dependency in your pom.xml file.

 

or, Gradle Dependency :

You can just add this to your dependencies within build.gradle file.

 

Let us start by generating a simple CSV file “student.csv” in the following program.

Program to generate a simple CSV file :

This program generates the “student.csv” file with the content as follows:

 

Let us now read the content of the generated CSV file using the following program.

Program to read the CSV file :

 

The output will be generated on console as follows :

 

Leave a Reply