🏠JavaApache Commons CSV to Read and Write CSV files in Java

Apache Commons CSV to Read and Write CSV files in Java

Let’s take a look at reading and writing CSV files in java using Apache Commons CSV with a few examples. First, you might ask why we need a library when we can write CSV files using pure java. To some extent you are right. However, you would need a library as your custom written reader/writer might not cover all edge cases.

What is apache-commons CSV?

Apache Commons CSV is a java library that reads and writes files in Comma Separated Value (CSV) format. This library can write in all supported variations of CSV files like MSExcel, Informix, Oracle, MySQL and TSV(Tab-separated values). You can also create custom CSV formats using its fluent API. Let’s look at this library in action with some examples.

Maven Dependency

The easiest way to set up apache-commons-csv is to add its appropriate maven dependency. At the time of writing, the current version is 1.8. Add the following snippet to your <dependencies> section and you are done.

</dependency>Code language: HTML, XML (xml)

Writing CSV files

Here is a simple java example of writing data into a file using Apache Commons CSV.

String[][] employees = {
        {"Man", "Sparkes", "[email protected]", "Engineering"},
        {"Dulcinea", "Terzi", "[email protected]", "Engineering"},
        {"Tamar", "Bedder", "[email protected]", "Legal"},
        {"Vance", "Scouller", "[email protected]", "Sales"},
        {"Gran", "Jagoe", "[email protected]", "Business Development"}
//create a CSV printer
CSVPrinter printer = new CSVPrinter(new FileWriter("employees.csv"), CSVFormat.DEFAULT);
//create header row
printer.printRecord("FirstName", "LastName", "Email", "Department");
// create data rows
for (String[] employee : employees) {
//close the printer after the file is complete
printer.close();Code language: Java (java)

Here, we have the employee data inside an array of arrays. To write this data into file, you just need to create CSVPrinter. And then you can simply start writing records(rows).

If you want header row, then make sure you print them above all the data records.

Read data from CSV files using Apache Commons CSV

Reading data using this library is as easy as writing. Also, apache-commons CSV gives you a lot of options to read these files. Let’s take a look at few java examples with apache commons CSV.

The library comes with a CSVParser class that can read and convert each row into a list of CSVRecords. From this record object, we can gather data by column index.

CSVParser csvParser = new CSVParser(new FileReader("employees.csv"), CSVFormat.DEFAULT);
for (CSVRecord record : csvParser) {
    System.out.println(record.get(0) + "," + record.get(1) + "," + record.get(2) + "," + record.get(3));
}Code language: Java (java)

You could also gather record values by assigning each index a header string.

CSVParser csvRecordsWithHeader
        = CSVFormat.DEFAULT.withHeader("FirstName","LastName","Email")
        .parse(new FileReader("csvwithoutheader.csv"));
for (CSVRecord record : csvRecordsWithHeader) {
    System.out.println(record.get("FirstName") + "," + record.get("LastName") + "," + record.get("Email"));
}Code language: Java (java)

The first column will be assigned the “FirstName” and second column will be assigned to “LastName” and the third column can be referred to using “Email”. If you already have a header row, then you can simply use the “withFirstLineAsHeader()” method.

CSVParser csvRecordsWithFirstLineHeader
        = CSVFormat.DEFAULT.withFirstRecordAsHeader()
        .parse(new FileReader("csvwithheader.csv"));
for (CSVRecord record : csvRecordsWithHeader) {
    System.out.println(record.get("FirstName") + "," + record.get("LastName") + "," + record.get("Email"));
}Code language: Java (java)

If you have a java Enum containing all the header names, then you can even use that to get the values from records. This approach will avoid having to hardcode column names as string everywhere.

java enum as header values in Apache Common CSV
CSVParser csvRecordsWithEnum
        = CSVFormat.DEFAULT.withHeader(Headers.class)
        .parse(new FileReader("employees.csv"));
for (CSVRecord record : csvRecordsWithEnum) {
    System.out.println(record.get(Headers.FirstName) + "," + record.get(Headers.Email));
}Code language: Java (java)

Here you need to note a couple of things. The apache-commons CSV library still allows you to access each column using their respective indexes. The header Enums are evaluated in the order of Enum.values() method.

Also, All these examples use the DEFAULT CSV format. You can take a look at the CSVFormat class to know more about other formats. They all have the same behaviour but differs slightly like if they accept line break, empty lines etc.

You can find all of these examples in our GitHub repository.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *