Last Updated:

Reading and Writing a CSV File using SuperCSV in Java

In this article, we will look at an easy way to handle CSV files using the Super CSV library. Using simple examples, we will learn how to parse (read) data and create new CSV files. Recently, we looked at an easy way to handle CSV in Java without using a third-party library – How to read a CSV file in Java: Set split and read information using the Scanner class. But what about the creation of new CSV files, elementary support for encoding, processing of special characters? To do this, there is the SuperCSV library , a fast, convenient and free tool for processing CSV in Java.

Super CSV Library At a Glance

  • SuperCSV takes care of the encoding, proper handling of commas, quotation marks, spaces, and escape sequences according to the CSV specification.
  • SuperCSV allows the developer to use their own separator or simply choose one of the predefined configurations, including commas, tabs, semicolons, and others.
  • SuperCSV works with threads, that is, it gives maximum I/O control when working with CSV.
  • SuperCSV allows you to quickly and conveniently parse the date, apply formatting, and use regular expressions to convert values.

In the example below, we parse a CSV file and convert the read information into a list of Java objects. We'll also look at a quick way to create a new CSV file in Java.

Read a CSV file using the SuperCSV library

Let's create a test.csv file with the following content:

Now let's create a data model:

Now let's create a maven project and add a Super CSV dependency in the pom file.xml:

If you want to add a jar file to your project, download the latest version of the library from this link and follow the instructions for adding a jar file to the Intellij IDEA project.

Now let's write a parser with which we will read information from a csv file:

Please note that we ourselves specify how exactly csv should be processed: we explicitly specified handlers for each field and specified the rules for parsing information. For example, if the role field is null, the parser will take this for granted and continue processing the file simply by writing null.

Let's run the above program and see the results of parsing in the console:

Write a CSV file using the SuperCSV library

Creating a new CSV file with Super CSV is as easy as parsing the data, only now we'll work with the CsvBeanWriter class:

Note that I've added a header string to identify the cells - it's not required, so feel free if you don't need it.

Now let's run the program and look at the console:

It's important to note that when creating the handlers, we allowed nullable fields with , so Super CSV just added a void.

Optional()

We use it to indicate the uniqueness of each identifier, so if duplicate ids are found, Exception will be discarded.

UniqueHashCode()SuperCsvConstraintViolationException()

More information and examples of use can be found on the official SuperCSV page.

Subscribe to new articles on Java development!