CSV API for Java

David Turner picture David Turner · Sep 19, 2008 · Viewed 209.4k times · Source

Can anyone recommend a simple API that will allow me to use read a CSV input file, do some simple transformations, and then write it.

A quick google has found http://flatpack.sourceforge.net/ which looks promising.

I just wanted to check what others are using before I couple myself to this API.

Answer

Jay R. picture Jay R. · Sep 19, 2008

I've used OpenCSV in the past.

import au.com.bytecode.opencsv.CSVReader;

String fileName = "data.csv";
CSVReader reader = new CSVReader(new FileReader(fileName ));

// if the first line is the header String[] header = reader.readNext();
// iterate over reader.readNext until it returns null String[] line = reader.readNext();