Convert Java Bean Object to Csv using Jsefa Library in Android

In this tutorial I will show you how to serialize and deserialize Java Bean objects to a CSV file. To make life simpler we will be using a library that provides annotations. You can find the Jsefa library here.

JSefa (Java Simple exchange format api) is a simple library for stream-based serialization of java objects to XML, CSV, FLR or any other format and back again using an iterator-style interface independent of the serialization format.

Step 1: Include the Library in Your Gradle

Include the Jsefa library as following in your gradle file. Look at this article to find out why we are excluding the xmlpull library from the module.

dependencies {
    compile ('net.sf.jsefa:jsefa-android:1.1.0.RELEASE') {
        exclude group: 'xmlpull'
    }
}

Step 2: Create your Model Class

Create your model class and add annotations to it as follows. You simply have to annotate your Java Bean class with CsvDataType and its properties with CsvField annotations.

@CsvDataType
public class Person {
    @CsvField(pos = 1)
    String firstName;
    @CsvField(pos = 2)
    String lastName;

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

Step 3: Add a CsvConverter class

The CsvConverter class will have static functions to serialize and deserialize. Initially add the CSV delimiter and function to get CsvConfiguration to the class.

public class CsvConverter {
    private static final char CSV_DELIMITER = '|';

    @NonNull
    private static CsvConfiguration getCsvConfiguration() {
        CsvConfiguration config = new CsvConfiguration();
        config.setFieldDelimiter(CSV_DELIMITER);
        return config;
    }
}

Step 4: Add a Function to Serialize to CSV

We will be writing a generic function to that will be able to write any Java Bean object to CSV. Add the serializeToCsv function to CsvConverter class.

public static <T> void serializeToCsv(File file, List<T> objectList, Class<T> clazz) throws IOException, IOFactoryException {
        Serializer serializer = CsvIOFactory.createFactory(getCsvConfiguration(), clazz).createSerializer();
        FileWriter writer = new FileWriter(file);
        serializer.open(writer);
        for (Object object : objectList) {
            serializer.write(object);
        }
        writer.close();
        serializer.close(true);
    }

It takes a File and a List<T> as parameters and writes it to the file. To use the serialize function use it as

List<Person> people = new ArrayList<>();
CsvConverter.serializeToCsv(file, people, Person.class);

Step 5: Add a function to Deserialize from CSV

Adain a generic function to deserialize any CSV file to appropriate Java Bean Object.

public static <T> List<T> deserializeFromCsv(File file, Class<T> clazz) throws IOException {
        Deserializer deserializer = CsvIOFactory.createFactory(getCsvConfiguration(), clazz).createDeserializer();
        FileReader fileReader = new FileReader(file);
        deserializer.open(fileReader);
        List<T> objectList = new ArrayList<>();
        while (deserializer.hasNext()) {
            T object = deserializer.next();
            objectList.add(object);
        }
        return objectList;
    }

To use the deserialize function use it as

List<Person> people = CsvConverter.deserializeToCsv(file, Person.class);

Thats it. Its this simple to serialize and deserialize CSV. You dont need to take care of splitting CSV string yourself or building a CSV file using StringBuilder.

This content has been helpful to you?

Thanks for contributing!

Yes No