Super CSV Reader

Posted By : Harshit Verma | 29-Jun-2018

 It is used with POJO (Plain Old Java Object). It reads each line into a java bean class.

The dependency that must be required for using the super CSV reader

<dependency>
    <groupId>net.sf.supercsv</groupId>
    <artifactId>super-csv</artifactId>
    <version>2.1.0</version>
</dependency>

1). Preparing a sample CSV file :-

At very first step you just need a sample CSV file that you want to read. The CSV must follow by a comma separation. And the first line contains the header.

1. name,surName,age,birthDate
2.
abc,def,23,21-06-1994
3. ghi,
jklm,24,21-08-1994
4.
mnop,qrs,25,8-05-1994

2). Creating a POJO :-

The second step is to create a POJO class, and the parameters that must be defined in the POJO class are same as that of the CSV headers. Because the values are fetched and stores as per the name of the headers from the CSV file. Here in POJO, the empty constructor is required, it is responsible for the value set in the parameters. Make sure that the getter setter is present in your POJO.

import java.util.Date;

public class UserDetails {

    String name;
    String surName;
    int age;
    Date birthDate;

    public UserDetails() {
    }

    public UserDetails(String name, String surName, int age, Date birthDate) {
        this.name = name;
        this.surName = surName;
        this.age = age;
        this.birthDate = birthDate;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurName() {
        return surName;
    }

    public void setSurName(String surName) {
        this.surName = surName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
}

3). Making cell processors:-

This cell processor is used to provide the automation to convert the data type and use to enforce the constraints from the CSV when dealing with the  Java Bean properties.
Some example for the CSV processors are:-

1). Not Null
2). new ParseDate
3). new ParseDouble
4). new ParseBigDecimal

This all convert the value of the string to the corresponding selected format.
Example of the cell processors:-

CellProcessor[] processors = new CellProcessor[] {
        new NotNull(), // name
        new NotNull(), // surName
        new ParseInt(), // age
        new ParseDate("MM/dd/yyyy"), // birthDate 
};

4).  Now reading the CSV:-

Here we are defining the CSV file name in the very first line, the header must match with the bean  parameters

ICsvBeanReader beanReader = new CsvBeanReader(new FileReader(csvFileName),
                    CsvPreference.STANDARD_PREFERENCE);
 
String[] header = beanReader.getHeader(true);
Book bookBean = null;
 
while ((userBean = beanReader.read(UserDetail.class, header, processors)) != null) {
    // deals with each bean read
    String name = userBean.getName();
    String surName = userBean.getSurName();
    //...
}

here is the complete example

static void readCSVFile(String csvFileName) {
    ICsvBeanReader beanReader = null;
    CellProcessor[] processors = new CellProcessor[] {
        new NotNull(), // name
        new NotNull(), // surName
        new ParseInt(), // age
        new ParseDate("MM/dd/yyyy"), // birthDate 
};
 
    try {
        beanReader = new CsvBeanReader(new FileReader(csvFileName),
                CsvPreference.STANDARD_PREFERENCE);
        String[] header = beanReader.getHeader(true);
        Book bookBean = null;
       while ((userBean = beanReader.read(UserDetail.class, header, processors)) != null) {
    // deals with each bean read
       String name = userBean.getName();
       String surName = userBean.getSurName();
   // describing the all parameters  here
        } 
    } catch (FileNotFoundException ex) {
        System.err.println("Could not find the CSV file: " + ex);
    } catch (IOException ex) {
        System.err.println("Error reading the CSV file: " + ex);
    } finally {
        if (beanReader != null) {
            try {
                beanReader.close();
            } catch (IOException ex) {
                System.err.println("Error closing the reader: " + ex);
            }
        }
    }
}

About Author

Author Image
Harshit Verma

Harshit is a bright Web Developer with expertise in Java and Spring framework and ORM tools Hibernate.

Request for Proposal

Name is required

Comment is required

Sending message..