Programmer's Cookbook

Recipes for the practical programmer

Friday, June 13, 2008

 

Parsing Comma Seperated Values (CSV) in Java

I suggest using Java CSV Library, it is very easy to use. If you use Maven 2, you can use this to load it as a dependancy.


<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>


The code below reads in a CSV file, parsing one line at a time, splitting each line into the vals array.


CsvReader rdr = new CsvReader("simeFile.csv");
while (rdr.readRecord()) {
String[] vals = rdr.getValues();
...
System.out.println(vals.length);
// process here
}

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?