forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenCSVReadBeansEx.java
More file actions
39 lines (30 loc) · 1.16 KB
/
OpenCSVReadBeansEx.java
File metadata and controls
39 lines (30 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.zetcode;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder;
import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import com.zetcode.bean.Car;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class OpenCSVReadBeansEx {
public static void main(String[] args) throws IOException {
String fileName = "src/main/resources/cars.csv";
Path myPath = Paths.get(fileName);
try (BufferedReader br = Files.newBufferedReader(myPath,
StandardCharsets.UTF_8)) {
HeaderColumnNameMappingStrategy<Car> strategy
= new HeaderColumnNameMappingStrategy<>();
strategy.setType(Car.class);
CsvToBean<Car> csvToBean = new CsvToBeanBuilder<Car>(br)
.withMappingStrategy(strategy)
.withIgnoreLeadingWhiteSpace(true)
.build();
List<Car> cars = csvToBean.parse();
cars.forEach(System.out::println);
}
}
}