Chaitanya Ghumare
Chaitanya Ghumare

Reputation: 381

How to map csv file to pojo class in java

I am using Java Maven plugin. I want to fetch employee.csv file records in POJO class. This POJO class I am generating from employee.csv header and all fields of POJO class are String type. Now I want to map employee.csv to generated POJO class. My requirement is I don't want to specify column names manually because if I change CSV file then again I have to change my code so it should dynamically map with any file, for instance:

firstName,lastName,title,salary
john,karter,manager,54372

I want to map this to POJO which I already have.

public class Employee
{
  private String firstName;
  private String lastName;
  .
  .
  //getters and setters 
  //toString()
}

Upvotes: 3

Views: 13966

Answers (3)

Kazitanvirazad
Kazitanvirazad

Reputation: 1

Parsing CSV file and mapping it with Java classes is sometimes a very hectic work. There are many CSV parser libraries available which makes our lives easy but still they can create complexity in implementing them in our existing codebase. To resolve all these issues and make the parsing process more simpler we can use a new CSV parsing library called csv4pojo.

csv4pojo makes its implementation in our existing code simpler and it's a very lightweight Java library and processes the parsing very fast. It is capable of parsing millions of rows of data and can write same amount of data in the CSV file. It supports 15 different datatypes. Integer,String,Float,Double,Boolean,Long,Character,Class,Integer[],String[],Float[],Double[],Boolean[],Character[],Long[].

<dependencies>
    <dependency>
        <groupId>org.csv4pojoparser</groupId>
        <artifactId>csv4pojo</artifactId>
        <version>2.1.0</version>
    </dependency>
</dependencies>

Add this maven dependencies in your pom.xml and read the docs here.

Upvotes: 0

Jeronimo Backes
Jeronimo Backes

Reputation: 6289

uniVocity-parsers allows you to map your pojo easily.

class Employee {

    @Trim
    @LowerCase
    @Parsed
    private String firstName;

    @Parsed
    private String lastName;

    @NullString(nulls = { "?", "-" }) // if the value parsed in the quantity column is "?" or "-", it will be replaced by null.
    @Parsed(defaultNullRead = "0") // if a value resolves to null, it will be converted to the String "0".
    private Integer salary; // The attribute name will be matched against the column header in the file automatically.
    ...

}

To parse:

BeanListProcessor<Employee> rowProcessor = new BeanListProcessor<Employee>(Employee.class);

CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setRowProcessor(rowProcessor);
parserSettings.setHeaderExtractionEnabled(true);

CsvParser parser = new CsvParser(parserSettings);

//And parse!
//this submits all rows parsed from the input to the BeanListProcessor
parser.parse(new FileReader(new File("/path/to/your.csv"))); 

List<Employee> beans = rowProcessor.getBeans();

Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).

Upvotes: 5

mayank agrawal
mayank agrawal

Reputation: 658

you can use openCSV jar to read the data and then you can map the each column values with the class attributes. Due to security reason, i can not share my code with you.

Upvotes: -2

Related Questions