tags:

views:

8248

answers:

10
+13  Q: 

csv api for java

Hi everyone,

Can anyone recommend a simple API that will allow me to use read a csv input file, do some simple transformations, and then write it.

A quick google has found http://flatpack.sourceforge.net/ which looks promising.

I just wanted to check what others are using before I couple myself to this api.

thanks,

David.

+4  A: 

We use JavaCSV, it works pretty well

Mat Mannion
A: 

A quick Google also found http://sourceforge.net/projects/javacsv/ ...and a lot more. That being said, I'd be tempted to just parse a CSV file using Java's built in StringTokenizer instead. Reading and writing CSV is simple enough that using someone else's API might be overkill.

Brian
Reading CSV looks simple, but is easy to get wrong, e.g. newlines appearing in quoted strings.
finnw
Agreed. The spec is slightly more than just "comma seperation", additonally there are a few common "tweaks" to the spec that certain tools generate.
Cheekysoft
A: 

The CSV format sounds easy enough for StringTokenizer but it can become more complicated. Here in Germany a semicolon is used as a delimiter and cells containing delimiters need to be escaped. Your not going to handle that easily with StringTokenizer.

I would go for http://sourceforge.net/projects/javacsv

paul
+2  A: 

For the last enterprise application I worked on that needed to handle a notable amount of CSV -- a couple of months ago -- I used SuperCSV at sourceforge and found it simple, robust and problem-free.

Cheekysoft
+1 for SuperCSV, but it has some nasty bugs which aren't fixed yet, new bugs aren't handled currently, and the last release is almost two years old.But we are using a patched/modified version in production without any problems.
MRalwasser
+1  A: 

If you intend to read csv from excel, then there are some interesting corner cases. I can't remember them all, but the apache commons csv was not capable of handling it correctly (with, for example, urls).

Be sure to test excel output with quotes and commas and slashes all over the place.

daveb
+2  A: 

check out the one from apache

The URL for this is http://commons.apache.org/sandbox/csv/.
bmatthews68
+4  A: 
Jay R.
+4  A: 

I know this is an old question, but just wanted o cross reference it with a new thread on the subject:

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

kolrie
+1  A: 

The SuperCSV project directly supports the parsing and structured manipulation of CSV cells. From http://supercsv.sourceforge.net/codeExamples.html you'll find e.g.

given a class

public class UserBean {
    String username, password, street, town;
    int zip;

    public String getPassword() { return password; }
    public String getStreet() { return street; }
    public String getTown() { return town; }
    public String getUsername() { return username; }
    public int getZip() { return zip; }
    public void setPassword(String password) { this.password = password; }
    public void setStreet(String street) { this.street = street; }
    public void setTown(String town) { this.town = town; }
    public void setUsername(String username) { this.username = username; }
    public void setZip(int zip) { this.zip = zip; }
}

and that you have a CSV file with a header. Let's assume the following content

username, password,   date,        zip,  town
Klaus,    qwexyKiks,  17/1/2007,   1111, New York
Oufu,     bobilop,    10/10/2007,  4555, New York

You can then create an instance of the UserBean and populate it with values from the second line of the file with the following code

class ReadingObjects {
  public static void main(String[] args) throws Exception{
    ICsvBeanReader inFile = new CsvBeanReader(new FileReader("foo.csv"), CsvPreference.EXCEL_PREFERENCE);
    try {
      final String[] header = inFile.getCSVHeader(true);
      UserBean user;
      while( (user = inFile.read(UserBean.class, header, processors)) != null) {
        System.out.println(user.getZip());
      }
    } finally {
      inFile.close();
    }
  }
}

using the following "manipulation specification"

final CellProcessor[] processors = new CellProcessor[] {
    new Unique(new StrMinMax(5, 20)),
    new StrMinMax(8, 35),
    new ParseDate("dd/MM/yyyy"),
    new Optional(new ParseInt()),
    null
};
kbg
A: 

There is also CSV/Excel Utility. It assumes all thos data is table-like and delivers data from Iterators.

Frank