Read and Parse CSV File in Java using Apache Commons CSV

Tags: Apache Commons Apache Commons CSV CSVRecord CSVParser CSVFormat CSV File Java IO Java NIO

In this tutorial, we learn how to parse CSV files in a Java application using Apache Commons CSV library. Via different Java code examples we learn how to create the CSV parser from File, InputStream, Path or Reader objects.

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

Sample Data

For example, we have the CSV file located at D:\SimpleSolution\Customers.csv with content as below.

First Name,Last Name,Email,Phone Number
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

The following Java example programs to read this CSV file.

Parse CSV File from Java NIO Path object

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

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ParseCsvFromPathExample {
    public static void main(String... args) {
        try {
            String csvFileName = "D:\\SimpleSolution\\Customers.csv";
            CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase();

            Path path = Paths.get(csvFileName);
            CSVParser csvParser = CSVParser.parse(path, StandardCharsets.UTF_8, 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);
            }

            csvParser.close();
        } 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

Parse CSV File from Java IO File object

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

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class ParseCsvFromFileExample {
    public static void main(String... args) {
        try {
            String csvFileName = "D:\\SimpleSolution\\Customers.csv";
            CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase();

            File file = new File(csvFileName);
            CSVParser csvParser = CSVParser.parse(file, StandardCharsets.UTF_8, 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);
            }

            csvParser.close();
        } 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

Parse CSV File from Java IO InputStream object

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

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class ParseCsvFromInputStreamExample {
    public static void main(String... args) {
        try {
            String csvFileName = "D:\\SimpleSolution\\Customers.csv";
            CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase();

            InputStream inputStream = new FileInputStream(csvFileName);
            CSVParser csvParser = CSVParser.parse(inputStream, StandardCharsets.UTF_8, 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);
            }

            inputStream.close();
            csvParser.close();
        } 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

Parse CSV File from Java IO FileReader object

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;
import java.io.Reader;

public class ParseCsvFromReaderExample {
    public static void main(String... args) {
        try {
            String csvFileName = "D:\\SimpleSolution\\Customers.csv";
            CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase();

            Reader reader = new FileReader(csvFileName);
            CSVParser csvParser = CSVParser.parse(reader, 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);
            }

            reader.close();
            csvParser.close();
        } 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 an URL in Java using Apache Commons CSV

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

Write and Read CSV File 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