pom.xml
<dependency>
<groupId>com.github.mygreen</groupId>
<artifactId>super-csv-annotation</artifactId>
<version>2.2</version>
</dependency>
XXXCsvBean.java
import com.github.mygreen.supercsv.annotation.CsvBean;
import com.github.mygreen.supercsv.annotation.CsvColumn;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@CsvBean
@NoArgsConstructor
@AllArgsConstructor
@Data
public class XXXCsvBean {
@CsvColumn(number = 1)
private String user;
@CsvColumn(number = 2)
private String email;
}
XXX.java
try (CsvAnnotationBeanReader<XXXCsvBean> csvReader = getCsvReader(inputStream)) {
csvReader.getHeader(true); //Header line skip
XXXCsvBean readRecord;
while ((readRecord = csvReader.read()) != null) {
String user = readRecord.getUser();
// do something
}
}
--
private CsvAnnotationBeanReader<XXXCsvBean> getCsvReader(InputStream inputStream) {
return new CsvAnnotationBeanReader<>(XXXCsvBean.class,
new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)),
CsvPreference.STANDARD_PREFERENCE);
}
Recommended Posts