PL
Java - jak zapisać obiekt java do pliku CSV za pomocą biblioteki Jackson CSV
0 points
W tym poście będziemy zapisywać obiekt java do pliku CSV za pomocą procesora Jackson CSV.
Aby użyć biblioteki Jackson CSV, musimy dodać właściwość (property) jackson-dataformat-csv do 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>
Poniżej mamy 2 klasy musimy zapisać prostą klasę java UserDto do pliku CSV.
Upewnij się, że ustawiłeś poprawną ścieżkę i nazwę pliku jako dane wyjściowe.
Aby uruchomić ten przykład, skopiuj do swojego projektu te 2 pliki:
- UserCsvWriter
- UserDto
2.1 Logika
Logika, która zapisuje obiekt UserDto java pojo do pliku CSV z biblioteką Jackson CSV.
Korzystanie z klas:
- CsvMapper + CsvSchema z biblioteki jackson CSV
- ObjectWriter z biblioteki jackson databind
- JsonGenerator z rdzenia Jacksona
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
}
Wynik:
xxxxxxxxxx
1
Users saved to csv file under path:
2
C:\csv_tests\user_output.csv
2.2 Obiekt Pojo użytkownika - 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
}