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.
The code below reads in a CSV file, parsing one line at a time, splitting each line into the
<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
}