Handling CSV files in Java is efficient with the OpenCSV library. It provides simple methods to read and write CSV files. Below is a detailed guide to mastering CSV file handling using OpenCSV:
A Comma-Separated Values (CSV) file is just a normal plain-text file, store data in column by column, and split it by a separator (e.g normally it is a comma “, ”).
Table of Contents
Introduction to OpenCSV
Java language does not provide any native support for effectively handling CSV files. OpenCSV is a CSV parser library for Java. OpenCSV supports all the basic CSV-type operations you are want to do. Java 7 is currently the minimum supported version for OpenCSV.
Implementation Steps
1. Add OpenCSV dependency to Maven Project
Add OpenCSV dependency to the pom.xml. The latest version can be downloaded from here.
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.10</version>
</dependency>
2. Add OpenCSV dependency to Gradle Project
For Gradle Project, we can include the OpenCSV dependency as shown below.
// https://mvnrepository.com/artifact/com.opencsv/opencsv
implementation group: 'com.opencsv', name: 'opencsv', version: '5.10'
3. Read all data from CSV
3.1 For reading a CSV file we need CSVReader class.
Following are sample CSV file that we’ll read.

3.2. Create an object of FileReader class with CSV file as a parameter.
FileReader fileReader = new FileReader(filePath);
3.3. Create csvReader object passing file reader as a parameter
CSVReader csvReader = new CSVReader(fileReader);
3.4. CSVReader provides a method called readAll() to read all the records at once into a List.
List<String[]> records = csvReader.readAll();
3.5. Print the data
for (String[] record : records) {
for (String field : record) {
System.out.print(field + " ");
}
System.out.println();
}
The complete program is shown below:
package org.example;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
public class CSVReader_Demo {
public static void main(String[] args) {
String filePath = "C:\\Users\\Documents\\csv_test.csv";
try {
FileReader fileReader = new FileReader(filePath);
CSVReader csvReader = new CSVReader(fileReader);
List<String[]> records = csvReader.readAll();
for (String[] record : records) {
for (String field : record) {
System.out.print(field + " ");
}
System.out.println();
}
} catch (IOException | CsvException e) {
System.err.println("Error reading CSV file: " + e.getMessage());
}
}
}
The output of the above program is

4. Read CSV by Skipping first line
When we read csv file by default, header will not ignored, as shown in output of above codes. When we need to skip the first element in the list then we can specify start line while creating csvReader object.
CSVReader csvReader = new CSVReaderBuilder(fileReader)
.withSkipLines(1)
.build();
The complete program is shown below
package org.example;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
public class CSVReader_LineSkip_Demo {
public static void main(String[] args) {
String filePath = "C:\\Users\\Documents\\csv_test.csv";
try {
FileReader fileReader = new FileReader(filePath);
CSVReader csvReader = new CSVReaderBuilder(fileReader)
.withSkipLines(1)
.build();
List<String[]> records = csvReader.readAll();
for (String[] record : records) {
for (String field : record) {
System.out.print(field + " ");
}
System.out.println();
}
} catch (IOException | CsvException e) {
System.err.println("Error reading CSV file: " + e.getMessage());
}
}
}
The output of the above program is

5. Reading CSV File with different separator

CSV files can be separated with a delimiter other than a comma e.g. semi-colon, pipe etc. The following example shows how to read data of CSV file separated by a semi-colon character.
For Custom separator first CSVParser with specific parser character is created.
CSVParser csvParser = new CSVParserBuilder().withSeparator(',').build();
Then, we will create a csvReader object using the withCSVParser() method. We will do this along with the constructor. We provide the created parser object to the parameter of the withCSVParser() method. At last call build method to build object.
CSVReader csvReader = new CSVReaderBuilder(fileReader).withCSVParser(csvParser).build();
The complete program is shown below
package org.example;
import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
public class CSVReader_Seperator_Demo {
public static void main(String[] args) {
String filePath = "C:\\Users\\Documents\\csv_test.csv";
try {
FileReader fileReader = new FileReader(filePath);
CSVParser csvParser = new CSVParserBuilder().withSeparator(',').build();
CSVReader csvReader = new CSVReaderBuilder(fileReader).withCSVParser(csvParser).build();
List<String[]> records = csvReader.readAll();
for (String[] record : records) {
for (String field : record) {
System.out.print(field + " ");
}
System.out.println();
}
} catch (IOException | CsvException e) {
System.err.println("Error reading CSV file: " + e.getMessage());
}
}
}
The output of the above program is

Troubleshooting Common Errors in CSV Handling with OpenCSV:
- File Not Found: The specified CSV file path may be incorrect or the file might not exist. Double-check the file path and ensure the file is located in the correct directory.
- Incorrect Delimiters: The CSV uses a different delimiter than expected (e.g., semicolon instead of comma). Please specify the correct delimiter when initializing CSVReader using `CSVParser`.
- Empty or Malformed CSV File: CSV files without headers, inconsistent rows, or structural issues. Please perform sanity checks on CSV structure before processing.