Working with CSV in Java: A Complete Guide to Reading and Writing

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with CSV in Java: A Complete Guide

CSV (Comma-Separated Values) is one of the most popular formats for storing and exchanging tabular data. In Java, there are several ways to work with CSV files: from manual parsing to using specialized libraries. In this article, we will cover the main approaches, including standard Java tools and popular third-party libraries.



1. Standard Approach: Manual CSV Reading

For simple projects, you can do without third-party libraries. Use BufferedReader and String.split():



import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.List;

public class SimpleCSVReader { public static List<String[]> readCSV(String filePath) { List<String[]> rows = new ArrayList<>(); String line; try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { while ((line = br.readLine()) != null) { String[] values = line.split(","); rows.add(values); } } catch (IOException e) { e.printStackTrace(); } return rows; } public static void main(String[] args) { List<String[]> data = readCSV("data.csv"); for (String[] row : data) { System.out.println(String.join(" | ", row)); } }}


Disadvantages: does not handle quotes, escaping of commas within fields, or line breaks. For production, it's better to use libraries.



2. OpenCSV — Powerful and Simple Library

OpenCSV is one of the most popular libraries for working with CSV in Java. It solves all parsing issues: quotes, escaping, different delimiters.



Installation (Maven):

<dependency>    <groupId>com.opencsv</groupId>    <artifactId>opencsv</artifactId>    <version>5.9</version></dependency>


Reading CSV with OpenCSV:

import com.opencsv.CSVReader;import com.opencsv.exceptions.CsvValidationException;import java.io.FileReader;import java.io.IOException;

public class OpenCSVReader { public static void main(String[] args) { try (CSVReader reader = new CSVReader(new FileReader("data.csv"))) { String[] nextLine; while ((nextLine = reader.readNext()) != null) { System.out.println(String.join(" | ", nextLine)); } } catch (IOException | CsvValidationException e) { e.printStackTrace(); } }}


Writing CSV with OpenCSV:

import com.opencsv.CSVWriter;import java.io.FileWriter;import java.io.IOException;

public class OpenCSVWriter { public static void main(String[] args) { String[] header = {"Name", "Age", "Email"}; String[] row1 = {"Alice", "30", "alice@example.com"}; String[] row2 = {"Bob", "25", "bob@example.com"}; try (CSVWriter writer = new CSVWriter(new FileWriter("output.csv"))) { writer.writeNext(header); writer.writeNext(row1); writer.writeNext(row2); } catch (IOException e) { e.printStackTrace(); } }}


3. Apache Commons CSV — Flexibility and Performance

Apache Commons CSV is another powerful library with support for different formats (CSV, TSV, Excel).



Installation (Maven):

<dependency>    <groupId>org.apache.commons</groupId>    <artifactId>commons-csv</artifactId>    <ver                    

Recommendations