Languages
[Edit]
EN

Java - how to read CSV file into java object using Jackson CSV library - complex example with BigDecimal and enum

14 points
Created by:
Marcin
15740

1. Overview

In this post we will read CSV file into java pojo object using Jackson CSV processor.

This example contains BigDecimal and enum as ComplexUserDto properties.

To use Jackson CSV library we need to add jackson-dataformat-csv dependency to pom.xml.

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-csv</artifactId>
    <version>2.9.9</version>
</dependency>

2. Read CSV file into java object

Below we have all classes we need to read csv file. Ensure to create csv file on your disc and set correct path in ComplexUserCsvReader class.

To run this example copy to your project those 3 files:

  1. ComplexUserCsvReader
  2. ComplexUserDto
  3. users.csv

2.1 Logic

Logic which reads CSV file into java ComplexUserDto pojo object with Jackson CSV library.

Usage of classes:

  • CsvMapper + CsvSchema from jackson CSV library
  • MappingIterator from jackson databind library
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class ComplexUserCsvReader {

    public static void main(String[] args) throws IOException {

        // set correct path to csv file on your disc
        File csvFile = new File("C:\\csv_tests\\complex_users.csv");

        CsvMapper csvMapper = new CsvMapper();

        CsvSchema csvSchema = csvMapper
                .typedSchemaFor(ComplexUserDto.class)
                .withHeader()
                .withColumnSeparator(',')
                .withComments();

        MappingIterator<ComplexUserDto> complexUsersIter = csvMapper
                .readerWithTypedSchemaFor(ComplexUserDto.class)
                .with(csvSchema)
                .readValues(csvFile);

        List<ComplexUserDto> complexUsers = complexUsersIter.readAll();

        complexUsers.forEach(System.out::println);
    }
}

Output:

ComplexUserDto{id=1, name='Ann', age=30, favouriteFruit=ORANGE, money=10001.222}
ComplexUserDto{id=2, name='Seth', age=25, favouriteFruit=APPLE, money=5550.788}
ComplexUserDto{id=3, name='Tom', age=27, favouriteFruit=BANANA, money=400.999}

2.2 User Pojo object - ComplexUserDto

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.math.BigDecimal;

@JsonPropertyOrder({"id", "name", "age", "favouriteFruit", "amount"})
public class ComplexUserDto {

    enum Fruit {
        APPLE, BANANA, ORANGE
    }

    private Long id;
    private String name;
    private Integer age;
    private Fruit favouriteFruit;
    private BigDecimal money;

    public ComplexUserDto() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public Fruit getFavouriteFruit() {
        return favouriteFruit;
    }

    public void setFavouriteFruit(Fruit favouriteFruit) {
        this.favouriteFruit = favouriteFruit;
    }

    public BigDecimal getMoney() {
        return money;
    }

    public void setMoney(BigDecimal money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "ComplexUserDto{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", favouriteFruit=" + favouriteFruit +
                ", money=" + money +
                '}';
    }
}

2.3 Complex user CSV file

Save this file on disc as 'complex_users.csv' and change location in ComplexUserCsvReader class.

id,name,age,favouriteFruit,money
1,Ann,30,ORANGE,10001.222
2,Seth,25,APPLE,5550.788
3,Tom,27,BANANA,400.999

References

  1. jackson-dataformat-csv - maven
  2. Comma-separated values (CSV) - wiki
  3. CsvMapper - JavaDoc
  4. CsvSchema - JavaDoc
  5. MappingIterator - JavaDoc
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join