Processing CSV in Java

September 14, 2010

Unlike Python, Java doesn't have built in support for CSV files. I needed to process them for a recent project, and I decided to use opencsv from http://sourceforge.net/projects/opencsv/.

Using this, it was simple to process the CSV file line by line:

import au.com.bytecode.opencsv.CSVReader;

CSVReader lCsvReader = new CSVReader(new StringReader(lCsvFileContents));
List<String[]> lCsvFiles = lCsvReader.readAll();

Iterator<String[]> lIterator = lCsvFiles.iterator();
while (lIterator.hasNext())
{ 
  String[] lCsvRow = lIterator.next();
  // do something with the csv row
}

References

Tags: java csv opencsv