The CSV (Comma Separated Values) is a text-based format that separates fields with comma delimiter ,. Line in CSV files represents one record that usually ends with a line break. In this article, we are going to present several ways to read CSV file using plain Java, OpenCSV library, and dedicated solution based on annotations and reflection.
2. Example CSV file
CSV file used in this tutorial contains a list of usernames with additional information:
This is the CSV file viewed as spread sheet:
2. Read CSV file line by line using BufferedReader
Actually you can use any method available in plain Java to read a file line by line and then split fields using String.split(...) method to parse it.
In this example, we used BufferedReader that allows us to read a file line by line and also supports buffering. In the first step we read a line from the CSV file, and then we split it into tokens using String.split(",") method. The result object contains a list of lines with a list of values.
3. Using Scanner to read CSV file
The Scanner class can parse primitive types and strings using regular expressions. In the following example we use it to retrieve lines of the CSV file and parse it using comma delimiter:
As you can see this is a little bit complex way to parse a CSV file.
4. Using JDK 8 Files.readAllLines(...) method
Let's check solution available in JDK 8 using Files.readAllLines(...) method.
In this example, we make use of the Files class available in java.nio package and Java streams to map lines into splitted fields.
5. Using OpenCSV library
The OpenCSV is a library dedicated to working with CSV files. We can use it to read the CSV and parse it into Java structure:
In this example, we used the readNext() method in CSVReader to read the records from the file.
If we want to parse CSV files directly into Java POJO classes with fields that represent columns, we must implement a dedicated parser.
First, let's start with the dedicated annotation called CSVField that will be used to mark fields in POJO object:
Columns from our CSV file will be represented by CSVColumn enum object:
In POJO class we need to set annotations for all the fields that have a representation in the CSV file:
Magic happen in CSVAbstractParser class:
1) first we parse CSV file using Files.readAllLines(...) method,
2) we assume that first row in CSV file will be the header,
3) we search for the headers and fields that represent them in POJO classes,
4) we iterate over list of rows and set values for all fields annotated with CSVField,
5) parser returns a list of POJO objects.
The CSV parser dedicated for User entity:
To parse CSV file we need to create an instance of CSVUserParser and run parse(...) method with the path to the CSV file, and class that will be our row representation:
In this article, we showcased several ways to read and parse the CSV file in Java. We presented a plain Java solution and an external library solution using OpenCSV. In case you need to convert CSV file to POJO objects you could use our dedicated solution described in point 6.
As usual, the code used in this tutorial is available under GitHub.