Can You Recommend a Java Library for Reading (And Possibly Writing) CSV Files

Can you recommend a Java library for reading (and possibly writing) CSV files?

We have used
http://opencsv.sourceforge.net/
with good success

I also came across another question with good links:
Java lib or app to convert CSV to XML file?

java: csv...do I need a library?

No, you don't. And even reading/parsing can be easily done with a plain JRE.

CSV is a plain (ascii-)text format with only a few rules:

  • rows (objects) are separated with a \n
  • columns (fields, attributes) are spearated with a delimiter char (usually a comma, but define whatever you need)
  • row and column delimiters must not be part of the field values

Parsing CSV File in Java

You are trying to do "string".equals("<40") which is checking if the string "string" is equivalent to the string "<40", not if that value is less than 40.

Try converting the string at l[2] to an integer, and then doing the check:

int age = Integer.parseInt(l[2]);
if (l[0].equals("CityA") && l[1].equals("M") && age < 40) {
count++;
}

Read CSV with Scanner()

scanner.useDelimiter(",");

This should work.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TestScanner {

public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("/Users/pankaj/abc.csv"));
scanner.useDelimiter(",");
while(scanner.hasNext()){
System.out.print(scanner.next()+"|");
}
scanner.close();
}

}

For CSV File:

a,b,c d,e
1,2,3 4,5
X,Y,Z A,B

Output is:

a|b|c d|e
1|2|3 4|5
X|Y|Z A|B|

reading CSV through JAVA

If you want to do not run out of memory and go fast, you need to go down to java.nio. There are plenty questions in here that might help you.

Looking for a java CSV library that allows reading columns by name

I can't really speak to opencsv, but if you have a fixed number of fields you might be able to use the JavaBean binding to do something like this.

Super CSV however, definitely supports reading and writing CSV rows by name using CsvBeanReader, CsvDozerBeanReader or CsvMapReader (and their writing equivalents).

If you prefer to keep things simple and use a map (with the column name as key, and the column value as the value) then you could use CsvMapReader. There are examples on reading and writing with CsvMapReader on the Super CSV website.

A reading example:

 ICsvMapReader mapReader = new CsvMapReader(
new FileReader("foo.csv"), CsvPreference.EXCEL_PREFERENCE);
try {
final String[] headers = mapReader.getHeader(true);
Map<String, String> row;
while( (row = mapReader.read(headers)) != null) {
for (String header : headers) {
System.out.println(header + " is " + row.get(header));
}
}
} finally {
mapReader.close();
}

Writing is quite similar.

Can you recommend a Java library for reading (and possibly writing) CSV files?

We have used
http://opencsv.sourceforge.net/
with good success

I also came across another question with good links:
Java lib or app to convert CSV to XML file?



Related Topics



Leave a reply



Submit