Wednesday, February 04, 2009

Review of Java CSV libraries

There are many good CSV libraries for Java. Here are the the ones that are open-source and use commercial-friendly licenses like Apache or LGPL:
SuperCSV, OpenCSV, Flatpack, Java CSV

Surprisingly not many of them handle all the quirks of CSV format. A pretty good description of CSV format can be found here:
http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm.
This page also includes an example of CSV data that few libraries will be able to handle:

John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123
Comparing the libraries based on functionality, i.e. the ability to handle all the quirks of CSV format, it looks like SuperCSV is the winner.

Super CSV
http://supercsv.sourceforge.net/
Apache 2.0 license

Blocking issues:
None

Minor issues:
Spaces are unconditionally removed from fields.
E.g. if you have a record that looks like this: "a, b, c"
you would get back fields like "a", "b", "c"
instead of "a", " b", " c"

Open CSV
http://opencsv.sourceforge.net/
Apache 2.0 license

Blocking issue:
If fields are quoted, and spaces are used
with delimiters, the resulting fields may include quotes.
E.g. if you have a record that looks like this:
a, "b ""c""", d
your second field may look like
"b "c"
instead of
b "c"


Flatpack
http://flatpack.sourceforge.net/
Apache 2.0 license

Blocking issue:
Unable to handle double quotes followed by separator. E.g.:
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123

Minor issues: It uses just another logging library (slf4j)
Unusual configuration via XML
Steepest learning curve


Java CSV
http://sourceforge.net/projects/javacsv
LGPL

Blocking issues:
Doesn't understand delimiters embedded in quotes, e.g.
"a ""b"", c", "d"
will produce 3 fields instead of 2