CSV (Comma-Separated Values) files are a standard way of storing tabular data like spreadsheets or database exports. Whether you’re exporting reports, saving logs, or generating data for third-party integration, the ability to write CSV files efficiently is essential for many Java applications.
In this tutorial, you’ll learn how to write data to a CSV file in Java using OpenCSV — a lightweight, easy-to-use library for reading and writing CSVs. We’ll walk you through creating headers, adding rows, and exporting to a clean .csv
file.
This tutorial is beginner-friendly and ideal for backend developers, data engineers, or anyone building Java applications that need to output structured data.

⚙️ Add OpenCSV to Your Maven Project
First, include OpenCSV in your pom.xml
file:
1 2 3 4 5 6 7 8 |
<dependency> <groupid>com.opencsv</groupid> <artifactid>opencsv</artifactid> <version>5.9</version> </dependency> |
Make sure to refresh your Maven project to download the dependency.
🧾 Use Case: Writing Employee Data to CSV
We’ll write the following employee data to a file called employees.csv
:
ID | Name | Department |
---|---|---|
101 | Alice | Engineering |
102 | Bob | Marketing |
103 | Charlie | Finance |
🧑💻 Java Code Example
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 33 34 35 36 37 38 |
package com.kscodes.opencsv; import com.opencsv.CSVWriter; import java.io.FileWriter; import java.io.IOException; public class WriteCSVExample { public static void main(String[] args) { String filePath = "employees.csv"; try (CSVWriter writer = new CSVWriter(new FileWriter(filePath))) { // Writing header String[] header = { "ID", "Name", "Department" }; writer.writeNext(header); // Writing rows String[ ] emp1 = { "101", "Alice", "Engineering" }; String[ ] emp2 = { "102", "Bob", "Marketing" }; String[ ] emp3 = { "103", "Charlie", "Finance" }; writer.writeNext(emp1); writer.writeNext(emp2); writer.writeNext(emp3); System.out.println("CSV file written successfully to " + filePath); } catch (IOException e) { System.err.println("Error writing CSV: " + e.getMessage()); e.printStackTrace(); } } } |
📝 Output (employees.csv)
1 2 3 4 5 6 7 |
ID,Name,Department 101,Alice,Engineering 102,Bob,Marketing 103,Charlie,Finance |
📌 Key Points
CSVWriter
handles commas, quotes, and escape characters for you.writeNext(String[])
is used to write a single row at a time.
💡 Tip: OpenCSV also supports custom separators and quote characters. This is useful if you’re working with
;
or|
delimited files.
📚 Real-World Applications
Writing CSV files is useful for:
- Exporting user reports
- Generating product feeds for e-commerce
- Creating logs for data processing pipelines
- Interfacing with legacy systems that consume CSV
OpenCSV makes it easy and safe by abstracting the low-level logic you’d otherwise write manually.
🧠 Conclusion
In this tutorial, you learned how to write Data to a CSV File using OpenCSV. From defining headers to adding rows, the process is straightforward and highly customizable.
By leveraging OpenCSV, you can handle edge cases like quotes, newlines, or special characters without any additional effort. If you’re exporting data in Java, OpenCSV should be your go-to tool.