EN
Java - how to write java object to CSV file using Jackson CSV library
1 points
In this post we will save java object into CSV file using Jackson CSV processor.
To use Jackson CSV library we need to add jackson-dataformat-csv dependency to pom.xml.
xxxxxxxxxx
1
<dependency>
2
<groupId>com.fasterxml.jackson.dataformat</groupId>
3
<artifactId>jackson-dataformat-csv</artifactId>
4
<version>2.9.9</version>
5
</dependency>
Below we have 2 classes we need to save simple UserDto java class to CSV file.
Ensure to set correct path and filename as output.
To run this example copy to your project those 2 files:
- UserCsvWriter
- UserDto
2.1 Logic
Logic which saves UserDto java pojo object to CSV file with Jackson CSV library.
Usage of classes:
- CsvMapper + CsvSchema from jackson CSV library
- ObjectWriter from jackson databind library
- JsonGenerator from jackson core
xxxxxxxxxx
1
import com.fasterxml.jackson.core.JsonGenerator;
2
import com.fasterxml.jackson.databind.ObjectWriter;
3
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
4
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
5
6
import java.io.File;
7
import java.io.IOException;
8
import java.util.ArrayList;
9
import java.util.Arrays;
10
import java.util.List;
11
12
public class UserCsvWriter {
13
14
public static void main(String[] args) throws IOException {
15
16
// set correct directory as output
17
File csvOutputFile = new File("C:\\csv_tests\\user_output.csv");
18
19
UserDto userDto1 = new UserDto(1L, "Ann", 30);
20
UserDto userDto2 = new UserDto(2L, "Seth", 28);
21
UserDto userDto3 = new UserDto(3L, "Emm", 24);
22
23
List<UserDto> list = new ArrayList<>(Arrays.asList(userDto1, userDto2, userDto3));
24
25
CsvMapper mapper = new CsvMapper();
26
mapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
27
28
CsvSchema schema = CsvSchema.builder().setUseHeader(true)
29
.addColumn("id")
30
.addColumn("name")
31
.addColumn("age")
32
.build();
33
34
ObjectWriter writer = mapper.writerFor(UserDto.class).with(schema);
35
36
writer.writeValues(csvOutputFile).writeAll(list);
37
38
System.out.println("Users saved to csv file under path: ");
39
System.out.println(csvOutputFile);
40
}
41
}
Output:
xxxxxxxxxx
1
Users saved to csv file under path:
2
C:\csv_tests\user_output.csv
2.2 User Pojo object - UserDto
xxxxxxxxxx
1
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
2
3
"id", "name", "age"}) ({
4
public class UserDto {
5
6
private Long id;
7
private String name;
8
private Integer age;
9
10
public UserDto() {
11
12
}
13
14
public UserDto(Long id, String name, Integer age) {
15
this.id = id;
16
this.name = name;
17
this.age = age;
18
}
19
20
public Long getId() {
21
return id;
22
}
23
24
public void setId(Long id) {
25
this.id = id;
26
}
27
28
public String getName() {
29
return name;
30
}
31
32
public void setName(String name) {
33
this.name = name;
34
}
35
36
public Integer getAge() {
37
return age;
38
}
39
40
public void setAge(Integer age) {
41
this.age = age;
42
}
43
44
45
public String toString() {
46
return "UserDto{" +
47
"id=" + id +
48
", name='" + name + '\'' +
49
", age=" + age +
50
'}';
51
}
52
}