Opencsv将csv文件转成Bean

Maven依赖

    
      com.opencsv
      opencsv
      5.2
    

CsvToBeanBuilder

优雅的解析文档中的字段。将CSV文件转换为Bean对象。

opencsv提供了基于"策略"的映射,将CSV绑定到bean。


16013479-954cb1d0f5887cab.png
接口名 策略
MappingStrategy 顶级的映射接口
headerColumnNameMappingStrategy 基于列名的映射策略,读取csv文件的第一行作为header,比如header1,header2,header3然后调用bean的setHeader1方法,setHeader2方法,setHeader3方法分别设置值。**所以这种策略要求,列名与bean中的属性名完全一致,如果不一致,则值为空,不会出错。使用注解时,注解名字必须与csv中列名一致。
ColumnPositionMappingStrategy 列位置映射策略,他没有header的概念,所以会输出取所有行。在columnMapping数组中指定bean的属性,第一个值对应csv的第一列,第二个值对应csv的第二列……
HeaderColumnNameTranslateMappingStrategy 列头名字翻译映射策略,与HeaderColumnNameMappintStrategy相比,bean的属性名可以与csv列头不一样。通过指定map来映射。

注:bean的类型只能为基本数据类型以及String类型,若是BigDecimal类型,那么将会抛出异常。

Bean

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CsvBean {
    //@CsvBindByName(column = "PLEG_FLTNO")
    @CsvBindByPosition(position = 0)
    private String PLEG_FLTNO;

    //@CsvBindByName(column = "PLEG_AP_THR_DEP")
    @CsvBindByPosition(position = 1)
    private String PLEG_AP_THR_DEP;

    //@CsvBindByName(column = "PLEG_AP_THR_ARR")
    @CsvBindByPosition(position = 2)
    private String PLEG_AP_THR_ARR;

    //@CsvBindByName(column = "PLEG_DATE")
    @CsvBindByPosition(position = 3)
    private String PLEG_DATE;

    //@CsvBindByName(column = "PLEG_LON")
    @CsvBindByPosition(position = 4)
    private String PLEG_LON;
}

csv文件:

"PLEG_FLTNO","PLEG_AP_THR_DEP","PLEG_AP_THR_ARR","PLEG_DATE","PLEG_LON","PLEG_LAT","PLEG_ALT","PLEG_SPE","PLEG_DIR","PLEG_TM_SEN","PLEG_UPDATE_TIME"
CA1658,SHE,PEK,"2019-12-01","122.388744","41.6808833","7193.28","618.966","278.53","2019-12-01 15:52:11","2019-12-01 15:54:15"
CA1658,SHE,PEK,"2019-12-01","122.255966","41.695625","7193.28","613.44","278.44","2019-12-01 15:53:14","2019-12-01 15:55:10"
CA1658,SHE,PEK,"2019-12-01","122.128377","41.7096805","7193.28","608.292","277.65","2019-12-01 15:54:16","2019-12-01 15:55:30"
CA1658,SHE,PEK,"2019-12-01","121.994802","41.7067388","7193.28","596.826","266.89","2019-12-01 15:55:21","2019-12-01 15:57:15"

基于列索引的映射

通俗点就是列位置映射,csv文件中列位置对应到bean中的列。
需要注意的是,该策略会输出所有的行,故,我们需要跳过某些行。

    @Test
    public void readCsvByPosition() throws Exception
    {
        String fileName="C:\\Users\\lyh\\Desktop\\CA1658SHE-PEK.csv";
        InputStreamReader is = new InputStreamReader(new  FileInputStream(fileName), StandardCharsets.UTF_8);
        ColumnPositionMappingStrategy mappingStrategy = new ColumnPositionMappingStrategy<>();
        mappingStrategy.setType(CsvBean.class);
        CsvToBean build = new CsvToBeanBuilder(is)
                .withMappingStrategy(mappingStrategy)
                .withSeparator(CSVWriter.DEFAULT_SEPARATOR)
                .withSkipLines(1)   //跳过第一行
                .build();
        List personList = build.parse();
        personList.forEach(System.out::println);
    }

结果

CsvBean(PLEG_FLTNO=CA1658, PLEG_AP_THR_DEP=SHE, PLEG_AP_THR_ARR=PEK, PLEG_DATE=2019-12-01, PLEG_LON=122.388744)
CsvBean(PLEG_FLTNO=CA1658, PLEG_AP_THR_DEP=SHE, PLEG_AP_THR_ARR=PEK, PLEG_DATE=2019-12-01, PLEG_LON=122.255966)
CsvBean(PLEG_FLTNO=CA1658, PLEG_AP_THR_DEP=SHE, PLEG_AP_THR_ARR=PEK, PLEG_DATE=2019-12-01, PLEG_LON=122.128377)
CsvBean(PLEG_FLTNO=CA1658, PLEG_AP_THR_DEP=SHE, PLEG_AP_THR_ARR=PEK, PLEG_DATE=2019-12-01, PLEG_LON=121.994802)

基于列名的映射

    @Test
    public void readCsvByName() throws Exception
    {
        String fileName="C:\\Users\\lyh\\Desktop\\CA1658SHE-PEK.csv";
        InputStreamReader is = new InputStreamReader(getInputStream(new  FileInputStream(fileName)), StandardCharsets.UTF_8);
        HeaderColumnNameMappingStrategy mappingStrategy = new HeaderColumnNameMappingStrategy<>();
        mappingStrategy.setType(CsvBean.class);
        CsvToBean build = new CsvToBeanBuilder(is)
                .withMappingStrategy(mappingStrategy)
                .withSeparator(CSVWriter.DEFAULT_SEPARATOR)
                .build();
        List personList = build.parse();
        personList.forEach(System.out::println);
    }

    /**
     * 读取流中前面的字符,看是否有bom,如果有bom,将bom头先读掉丢弃
     *  (opencsv 按列名获取bean对象,第一列缺失的情况)
     */
    public static InputStream getInputStream(InputStream in) throws IOException {

        PushbackInputStream testin = new PushbackInputStream(in);
        int ch = testin.read();
        if (ch != 0xEF) {
            testin.unread(ch);
        } else if ((ch = testin.read()) != 0xBB) {
            testin.unread(ch);
            testin.unread(0xef);
        } else if (testin.read() != 0xBF) {
            throw new IOException("错误的UTF-8格式文件");
        }
        return testin;
    }

结果

CsvBean(PLEG_FLTNO=CA1658, PLEG_AP_THR_DEP=SHE, PLEG_AP_THR_ARR=PEK, PLEG_DATE=2019-12-01, PLEG_LON=122.388744)
CsvBean(PLEG_FLTNO=CA1658, PLEG_AP_THR_DEP=SHE, PLEG_AP_THR_ARR=PEK, PLEG_DATE=2019-12-01, PLEG_LON=122.255966)
CsvBean(PLEG_FLTNO=CA1658, PLEG_AP_THR_DEP=SHE, PLEG_AP_THR_ARR=PEK, PLEG_DATE=2019-12-01, PLEG_LON=122.128377)
CsvBean(PLEG_FLTNO=CA1658, PLEG_AP_THR_DEP=SHE, PLEG_AP_THR_ARR=PEK, PLEG_DATE=2019-12-01, PLEG_LON=121.994802)

转换器

在csv获取的都是字符串,这种情况下应该使用转换器。将csv中的字段转换为对应的bean中的字段类型。

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CsvBean {
    //@CsvBindByName(column = "PLEG_FLTNO")
    @CsvBindByPosition(position = 0)
    private String PLEG_FLTNO;

    //@CsvBindByName(column = "PLEG_AP_THR_DEP")
    @CsvBindByPosition(position = 1)
    private String PLEG_AP_THR_DEP;

    //@CsvBindByName(column = "PLEG_AP_THR_ARR")
    @CsvBindByPosition(position = 2)
    private String PLEG_AP_THR_ARR;

    //@CsvBindByName(column = "PLEG_DATE")
    //@CsvBindByPosition(position = 3)
    @CsvCustomBindByPosition(position = 3,converter = ConvertToDate.class)
    private LocalDate PLEG_DATE;

    //@CsvBindByName(column = "PLEG_LON")
    @CsvBindByPosition(position = 4)
    private String PLEG_LON;
}
public class ConvertToDate extends AbstractBeanField {
    @Override
    protected Object convert(String s) throws CsvDataTypeMismatchException, CsvConstraintViolationException {
        return LocalDate.parse(s, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }
}

注:若是列映射策略,则要使用@CsvCustomBindByPosition()注解。
注:若是名字映射策略,则要使用@CsvCustomBindByName()注解。

过滤器

opencsv提供了过滤器,可以过滤某些行,比如page header、page footer等

所有的过滤器必须实现CsvToBeanFilter 接口

public class CsvFilter implements CsvToBeanFilter {
    @Override
    public boolean allowLine(String[] strings) {
        return strings[4].equalsIgnoreCase("122.388744");
    }
}

CsvToBeanFilter 是一个函数式接口所以也可以直接简化为lambda表达式

    @Test
    public void readCsvByPosition() throws Exception
    {
        String fileName="C:\\Users\\lyh\\Desktop\\CA1658SHE-PEK.csv";
        InputStreamReader is = new InputStreamReader(new  FileInputStream(fileName), StandardCharsets.UTF_8);
        ColumnPositionMappingStrategy mappingStrategy = new ColumnPositionMappingStrategy<>();
        mappingStrategy.setType(CsvBean.class);
        CsvToBean build = new CsvToBeanBuilder(is)
                .withMappingStrategy(mappingStrategy)
                .withSeparator(CSVWriter.DEFAULT_SEPARATOR)
                .withSkipLines(1)
                .withFilter(param-> param[4].equalsIgnoreCase("122.388744"))  //增加过滤器
                //.withFilter(new CsvFilter())
                .build();
        List personList = build.parse();
        personList.forEach(System.out::println);
    }

你可能感兴趣的:(Opencsv将csv文件转成Bean)