CSV (Comma-Separated Values) is one of the most widely used data exchange formats. Whether you’re importing data from spreadsheets, integrating with third-party systems, or processing simple reports, chances are you’ll come across CSV files in your Java projects.
However, reading CSV files manually with BufferedReader
and String.split()
can quickly become messy, especially when dealing with quoted values, commas within fields, or line breaks. This is where OpenCSV shines.
In this tutorial, you’ll learn how to read a simple CSV file using OpenCSV in a clean and reliable way. This guide is perfect for beginners working on Java console applications, data loaders, or backend services.

⚙️ Setting Up OpenCSV in Maven
To get started, add the following dependency in your pom.xml
. This will bring in the OpenCSV library, which handles CSV parsing under the hood.
1 2 3 4 5 6 7 8 |
<dependency> <groupId>com.opencsv</groupId> <artifactId>opencsv</artifactId> <version>5.9</version> </dependency> |
Make sure your Java version is Java 8 or higher.
📁 Sample CSV File
Create a file named employees.csv
in your project root (or resources folder) with the following content:
1 2 3 4 5 6 7 |
ID,Name,Department 101,Alice,Engineering 102,Bob,Marketing 103,Charlie,Finance |
Each line represents a record, and fields are separated by commas. The first row is a header row.
🧑💻 Java Code to Read CSV Using OpenCSV
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.kscodes.opencsv; import com.opencsv.CSVReader; import java.io.FileReader; import java.util.List; public class ReadCSVExample { public static void main(String[] args) { String filePath = "employees.csv"; try (CSVReader reader = new CSVReader(new FileReader(filePath))) { List<String[]> records = reader.readAll(); for (int i = 0; i < records.size(); i++) { String[] row = records.get(i); if (i == 0) { System.out.println("Header: " + String.join(" | ", row)); } else { System.out.printf("Employee -> ID: %s | Name: %s | Dept: %s%n", row[0], row[1], row[2]); } } } catch (Exception e) { System.err.println("Error reading CSV file: " + e.getMessage()); e.printStackTrace(); } } } |
🔍 Code Explanation
CSVReader
: This class simplifies reading CSV files, handling comma delimiters and quoted fields automatically.readAll()
: Loads the entire file into memory. Great for small-to-medium files.- Loop logic: We print the header differently to give meaningful structure to the output.
- Error Handling: It’s essential to catch exceptions like
FileNotFoundException
or malformed CSV issues.
✅ Tip: For large files, use
readNext()
inside awhile
loop instead ofreadAll()
to avoid high memory usage.
🧠 Real-world Use Cases
Reading CSV files is common in:
- Importing user data into admin panels
- Loading configuration settings
- Processing log or audit files
- Migrating legacy data into a modern app
- Parsing product feeds for e-commerce apps
OpenCSV makes these tasks not only easier but also safer and more maintainable.
🧾 Output Example
1 2 3 4 5 6 7 8 |
Header: ID | Name | Department Employee -> ID: 101 | Name: Alice | Dept: Engineering Employee -> ID: 102 | Name: Bob | Dept: Marketing Employee -> ID: 103 | Name: Charlie | Dept: Finance |
🧩 Conclusion
Reading CSV files manually can lead to bugs, especially when data gets messy. OpenCSV gives Java developers a robust and flexible tool to handle CSV files with ease. In this post, you learned how to:
- Set up OpenCSV in a Maven project
- Create and read a sample CSV file
- Use
CSVReader
to parse rows - Handle headers and display results
This is just the beginning! In future posts, you’ll explore writing CSV files, mapping to Java beans, handling custom delimiters, and more advanced operations.