Write and Read CSV File in Java using Apache Commons CSV

Tags: Apache Commons Apache Commons CSV CSVPrinter CSVParser CSVFormat CSV CSVRecord

In this tutorial, we are going to learn how to write and read CSV files in Java applications using Apache Commons CSV library.

Add Apache Commons CSV library to your Java project

To use Apache Commons CSV Java library in the Gradle build project, add the following dependency into the build.gradle file.

compile group: 'org.apache.commons', name: 'commons-csv', version: '1.8'

To use Apache Commons CSV Java library in the Maven build project, add the following dependency into the pom.xml file.

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

To download the Apache Commons CSV jar file you can visit Apache Commons CSV download page at commons.apache.org

Write CSV File using CSVPrinter class

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

import java.io.FileWriter;
import java.io.IOException;

public class WriteCsvFileExample {
    public static void main(String... args) {
        String csvFileName = "D:\\SimpleSolution\\Customers.csv";
        String[] headers = new String[] {"First Name", "Last Name", "Email", "Phone Number"};

        CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(headers);

        try(FileWriter fileWriter = new FileWriter(csvFileName);
            CSVPrinter csvPrinter = new CSVPrinter(fileWriter, csvFormat)) {

            csvPrinter.printRecord("John", "Doe", "john@simplesolution.dev", "123-456-789");
            csvPrinter.printRecord("Emerson", "Wilks", "emerson@simplesolution.dev", "123-456-788");
            csvPrinter.printRecord("Wade", "Savage", "wade@simplesolution.dev", "123-456-787");
            csvPrinter.printRecord("Star", "Lott", "star@simplesolution.dev", "123-456-786");
            csvPrinter.printRecord("Claudia", "James", "claudia@simplesolution.dev", "123-456-785");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Run the application above, you will receive a new CSV file located in D:\SimpleSolution\Customers.csv with data as below screenshot.

Write and Read CSV File in Java using Apache Commons CSV

Read CSV File using CSVParser class

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

import java.io.FileReader;
import java.io.IOException;

public class ReadCsvFileExample {
    public static void main(String... args) {
        String csvFileName = "D:\\SimpleSolution\\Customers.csv";

        CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase();

        try(FileReader fileReader = new FileReader(csvFileName);
            CSVParser csvParser = new CSVParser(fileReader, csvFormat)) {

            for(CSVRecord csvRecord : csvParser) {
                String firstName = csvRecord.get("First Name");
                String lastName = csvRecord.get("Last Name");
                String email = csvRecord.get("Email");
                String phoneNumber = csvRecord.get("Phone Number");

                System.out.println(firstName + "," + lastName + "," + email + "," + phoneNumber);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:
John,Doe,john@simplesolution.dev,123-456-789
Emerson,Wilks,emerson@simplesolution.dev,123-456-788
Wade,Savage,wade@simplesolution.dev,123-456-787
Star,Lott,star@simplesolution.dev,123-456-786
Claudia,James,claudia@simplesolution.dev,123-456-785

Happy Coding 😊

Read and Parse CSV Content from a String in Java using Apache Commons CSV

Top 3 Libraries for Writing and Reading CSV File in Java

Spring Boot Web Application Download CSV File

Escape or Unescape String for CSV column data in Java using Apache Commons Text