Boost your Production at CSV files with Apache.commons.CSV in Java

Petros Koulianos 💀☠👽
Javarevisited
Published in
3 min readMar 4, 2020
Photo by Kristopher Roller on Unsplash

1. Introduction

CSV (comma-separated values) files are used widely to exchange data between applications. However, operations with CSV files can be tricky and time-consuming.

Apache Software Foundation gives us the Apache.Commons.CSV library that makes our life easier with CSV files READ/WRITE operations. Thus it comes with Apache License 2.0.

CSV files have various formats, in the following example we will work with classic RFC4180 format.

RFC4180 format specifications according to wikipedia :

Is plain text using a character set such as ASCII, various Unicode character sets (e.g. UTF-8), EBCDIC, or Shift JIS,

Consists of records (typically one record per line),

With the records divided into fields separated by delimiters (typically a single reserved character such as comma, semicolon, or tab; sometimes the delimiter may include optional spaces),

Where every record has the same sequence of fields.

2. Read CSV files with Apache Commons CSV

Create a new maven project with name ParseCSV and paste the following snippet to your ParseCSV/pom.xml file :

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

The above dependency will download the necessary library for our example.

Download a sample CSV file with countries and paste it into your project directory ParseCSV/countries.csv.

Add to your main method the following code:

try {Reader csvData = new FileReader("countries.csv");CSVParser parser = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(csvData);for (CSVRecord csvRecord : parser) {System.out.println(csvRecord.getRecordNumber()+":"+csvRecord.get("name"));}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}

Lets explain the above code snippet :

First, we create a Reader object to read the countries.csv file.

Reader csvData = new FileReader("countries.csv");

We use CSVFormat static object to create a CSVParser and parse the CSV file with RFC4180 format and First Record as Header.

CSVParser parser = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(csvData);

Finally, iterate parser object to get the data from the file.

for (CSVRecord csvRecord : parser) {System.out.println(csvRecord.getRecordNumber()+":"+csvRecord.get("name"));}

Apache.Commons.CSV offers several methods to read fields at CSV files, in this example, we access the data with header names but it can also be done with index numbers.

3. Write CSV files with Apache Commons CSV

Writing CSV files with Apache.Commons.CSV is just as easy .

Copy and paste the following code snippet :

try {FileWriter fileWriter = new FileWriter("./mountains.csv", true);try (CSVPrinter csvPrinter = new CSVPrinter(fileWriter, CSVFormat.RFC4180)) {//HEADER recordcsvPrinter.printRecord("mountain","meters");//DATA recordscsvPrinter.printRecord("Mount Everest","8.848");csvPrinter.printRecord("K2","8.611");csvPrinter.printRecord("Kangchenjunga","8.586");csvPrinter.printRecord("olumpus","2.918");csvPrinter.flush();}} catch (IOException e) {e.printStackTrace();}

Lets explain the above code snippet :

First step create a FileWriter in order to write to mountains.csv file

FileWriter fileWriter = new FileWriter("./mountains.csv", true);

Next, use CSVPrinter class to generate a csvWriter and start writing

CSVPrinter csvPrinter= new CSVPrinter(fileWriter, CSVFormat.RFC4180)

Finally its time to start writing data

//HEADER recordcsvPrinter.printRecord("mountain","meters");//DATA recordscsvPrinter.printRecord("Mount Everest","8.848");csvPrinter.printRecord("K2","8.611");csvPrinter.printRecord("Kangchenjunga","8.586");csvPrinter.printRecord("olumpus","2.918");csvPrinter.flush();

4. Conclusion

Apache.Commons.CSV offers a simple interface for READ/WRITE operations with CSV files.

However, Apache Commons CSV is one of the best libraries for its purpose with regular updates and extensive documentation.

--

--

Petros Koulianos 💀☠👽
Javarevisited

Software Engineer 👽 | Building applications for health industry | Work with JavaScript, Typescript, PHP | My Newsletter📩 at petran.substack.com