Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ā¤ šŸ’»
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

Java - how to read CSV file into java object using Jackson CSV library?

0 contributions
5 points

1. Overview

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

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Ā UserCsvReader class.

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

  1. UserCsvReader
  2. UserDto
  3. users.csv

2.1Ā Logic

Logic which reads CSV file into java UserDto 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 UserCsvReader {

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

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

        CsvMapper csvMapper = new CsvMapper();

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

        MappingIterator<UserDto> usersIter = csvMapper
                .readerWithTypedSchemaFor(UserDto.class)
                .with(csvSchema)
                .readValues(csvFile);

        List<UserDto> users = usersIter.readAll();

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

Output:

UserDto{id=1, name='Ann', age=30}
UserDto{id=2, name='Seth', age=25}
UserDto{id=3, name='Tom', age=27}

2.2 User Pojo object - UserDto

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({"id", "name", "age"})
public class UserDto {

    private Long id;
    private String name;
    private Integer age;

    public UserDto() {

    }

    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;
    }

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

2.3 User CSV file

Save this file on disc as 'users.csv' and change location inĀ UserCsvReader class.

id,name,age
1,Ann,30
2,Seth,25
3,Tom,27

References

  1. jackson-dataformat-csv - maven
  2. Comma-separated values (CSV)Ā - wiki
  3. CsvMapper - JavaDoc
  4. CsvSchema - JavaDoc
  5. MappingIterator - JavaDoc
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey šŸ‘‹
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more