Skip to content
ybonnel edited this page Aug 13, 2012 · 14 revisions

In this page you will find informations on basics usage of CsvEngine.

Map a class

/**
 * Class representing a Dog.
 * This class is mapped to a CSV File.
 */
// This annotation is use to declare Dog as a class mapped to a CSV File.
// You can change default separator with : @CsvFile(separator = ";")
@CsvFile
public class Dog {
	// The "name" field is mapped to a column in CSV named "name"
	@CsvColumn("name")
	private String name;
	// The "race" field is mapped to a column in CSV named "race"
	@CsvColumn("race")
	private String race;
	@CsvColumn("proprietary")
	// The "proprietary" field is mapped to a column in CSV named "proprietary"
	private String proprietary;
	@Override
	public String toString() {
		return "Dog{" +
				"name='" + name + '\'' +
				", race='" + race + '\'' +
				", proprietary='" + proprietary + '\'' +
				'}';
	}
}

Create the engine

CsvEngine engine = new CsvEngine(Dog.class);

Read a CSV File

List<Dog> dogs = engine.parseInputStream(csvFile, Dog.class).getObjects();

Write a CSV File

engine.writeFile(writer, dogs, Dog.class);

Read a CSV File and handle each row

engine.parseFileAndInsert(new InputStreamReader(stream), Dog.class, new InsertObject<Dog>() {
	@Override
	public void insertObject(Dog dog) {
		System.out.println(dog.toString());
	}
});

Samples

You can see samples in CsvEngineSample.