In the world of software development, handling data stored in CSV (Comma-Separated Values) files is quite common. Whether you’re importing user information, product lists, or analytics data—CSV is one of the simplest formats for data exchange. In Java, working with raw CSV files manually can be error-prone and tedious. This is where OpenCSV comes in.
In this Java OpenCSV tutorial, you’ll learn what OpenCSV is, why it’s useful, and how to use it to read and write CSV files with ease. Let’s get started!

📌 What is OpenCSV?
OpenCSV is a lightweight and powerful open-source Java library that simplifies the process of reading and writing CSV files. Instead of manually splitting strings and handling commas, quotes, and newlines, OpenCSV provides clean APIs and annotations to work with CSV data effortlessly.
✅ Why Use OpenCSV?
Here are some solid reasons to choose OpenCSV in your Java project:
- Easy to use and integrate
- Supports reading/writing CSV with custom delimiters
- Can bind CSV data to Java POJOs using annotations
- Handles quoted fields and multiline data
- Supports large file processing efficiently
⚙️ How to Set Up OpenCSV in a Maven Project
To start using OpenCSV, just add the following dependency to your pom.xml file:
|
1 2 3 4 5 6 7 8 9 |
<dependency> <groupId>com.opencsv</groupId> <artifactId>opencsv</artifactId> <version>5.9</version> </dependency> |
Make sure to update the version if a newer one is available.
📥 Reading a Simple CSV File
Let’s say we have a file called users.csv with this content:
|
1 2 3 4 5 6 |
Name,Email,Age Alice,alice@example.com,28 Bob,bob@example.com,32 |
Here’s a simple example to read this file using OpenCSV:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.kscodes.opencsv; import com.opencsv.CSVReader; import java.io.FileReader; import java.util.List; public class SimpleCSVReader { public static void main(String[] args) { try (CSVReader reader = new CSVReader(new FileReader("users.csv"))) { List<String[]> rows = reader.readAll(); for (String[] row : rows) { System.out.println(String.join(" | ", row)); } } catch (Exception e) { e.printStackTrace(); } } } |
📝 Writing Data to a CSV File
Now let’s see how to write data to a CSV file 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 |
package com.kscodes.opencsv; import com.opencsv.CSVWriter; import java.io.FileWriter; public class SimpleCSVWriter { public static void main(String[] args) { try (CSVWriter writer = new CSVWriter(new FileWriter("output.csv"))) { String[] header = { "Name", "Email", "Age" }; writer.writeNext(header); String[] user1 = { "Charlie", "charlie@example.com", "25" }; String[] user2 = { "Diana", "diana@example.com", "29" }; writer.writeNext(user1); writer.writeNext(user2); System.out.println("CSV written successfully."); } catch (Exception e) { e.printStackTrace(); } } } |
🧠 Conclusion
OpenCSV makes working with CSV files in Java a breeze. From simple file reads to complex bean mappings, this library has you covered. In this tutorial, you learned the basics: what OpenCSV is, why it’s useful, and how to read and write CSV files using it.
If you’re dealing with CSVs regularly, OpenCSV is a must-have tool in your Java toolkit. Stay tuned for more tutorials where we’ll explore advanced features like bean binding, custom separators, and CSV validation.