Java API to make an object from a CSV file

I prefer opencsv, it is ultra simple and very clean.

http://opencsv.sourceforge.net/

For example reading:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
    // nextLine[] is an array of values from the line
    System.out.println(nextLine[0] + nextLine[1] + "etc...");
}

JSefa allow you to annotate Java classes that can be used in a serialization and de-serialization process. The tutorial demonstrates how this works with the CsvIOFactory class.

(From the tutorial) Annotating a bean is as simple as specifying the locations of the items in the list of values, and if necessary, you'll need to specify the conversion format:

@CsvDataType()
public class Person {
    @CsvField(pos = 1)
    String name;

    @CsvField(pos = 2, format = "dd.MM.yyyy")
    Date   birthDate;
}

Tags:

Java

Csv