使用FlatFileItemReader读取csv文件时,有一列映射类型为Int,但该列在csv文件中为空(eg:.6321517,Jack, 1,,最后两列为空)。java.lang.NumberFormatException: Unparseable number解析文件时抛出异常( )
数据集
CUSTR_NBR,SUR_NAME,CHECK_FLAG,RESN_CODE
6321517,Jack,1,,
首先解析第一行数据(CUSTR_NBR,SUR_NAME,CHECK_FLAG,RESN_CODE),所以我设置.SetLinesToSkip(1). 但如果解析出空值,对应的“CHECK_FLAG”和“RESN_CODE”就无法正常执行。我相信有相应的配置项。我查看了springbatch的文档,没有找到相关的配置项。
csvItemReader
@Bean
@StepScope
public FlatFileItemReader<InfoDTO> csvItemReader() {
FlatFileItemReader<InfoDTO> csvItemReader = new FlatFileItemReader<>();
csvItemReader.setResource(new ClassPathResource("data/charge-off.csv"));
csvItemReader.setLinesToSkip(1);
DelimitedLineTokenizer tokenizer=new DelimitedLineTokenizer();
String[] tokens = new String[]{"CUSTR_NBR","SUR_NAME","CHECK_FLAG","RESN_CODE","EMPNO"};
tokenizer.setNames(tokens);
DefaultLineMapper<InfoDTO> lineMapper=new DefaultLineMapper<InfoDTO>();
lineMapper.setLineTokenizer(tokenizer);
lineMapper.setFieldSetMapper(new InfoFileMapper());
lineMapper.afterPropertiesSet();
csvItemReader.setLineMapper(lineMapper);
return csvItemReader;
}
映射器
public class InfoFileMapper implements FieldSetMapper<ChargeOffBatchDTO> {
@Override
public InfoDTO mapFieldSet(FieldSet fieldSet) throws BindException {
if(fieldSet == null){
return null;
}
return new InfoDTO(
fieldSet.readString("CUSTR_NBR"),
fieldSet.readString("SUR_NAME"),
fieldSet.readString("CHECK_FLAG"),
fieldSet.readInt("RESN_CODE"),
fieldSet.readInt("EMPNO")
);
}
}
我需要将空列映射为0值,如何配置?
小怪兽爱吃肉
慕标琳琳
相关分类