欢迎访问个人博客 德鲁大叔撸代码
最近在做一个对账系统,上游给的对账文件类型为csv的,那么要做对账的第一件事就是把上游文件中的数据解析出来,再跟我自己的系统去做对账。 当然想到了opencsv这个框架。
第一步:引入依赖
<!-- csv依赖 -->
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>4.5</version>
</dependency>
第二步:编写解析文件所对应的实体类 (后面解析的时候用的到)
@Data
public class TopPosFile extends BaseEntity {
static final String TRIM = "\\s*(\\S+)\\s*";
@CsvBindByPosition(position = 0,capture = TRIM)
private String ateId;
@CsvBindByPosition(position = 1,capture = TRIM)
private String transDate;
@CsvBindByPosition(position = 2,capture = TRIM)
private String sysTime;
@CsvBindByPosition(position = 3,capture = TRIM)
private String ordId;
@CsvBindByPosition(position = 4,capture = TRIM)
private String refNum;
@CsvBindByPosition(position = 5,capture = TRIM)
private String payCardId;
@CsvBindByPosition(position = 6,capture = TRIM)
private BigDecimal ordAmt;
private String arg1;
private String arg2;
}
第三步:编写方法解析文件
@Override
public void importPosFileData(String fileName) {
try(FileInputStream fis = new FileInputStream(fileName);
InputStreamReader inputStream = new InputStreamReader(fis,"utf-8")){
if(StringUtils.isNotBlank(fileName)){
log.info("开始解析刷卡交易对账文件,文件名:{}", fileName);
CSVReaderBuilder csvReaderBuilder = new CSVReaderBuilder(inputStream);
CSVParserBuilder csvParserBuilder = new CSVParserBuilder();
csvParserBuilder.withSeparator('|'); // 分割符 可以根据具体情况设置
csvReaderBuilder.withCSVParser(csvParserBuilder.build());
CSVReader reader = csvReaderBuilder.build();
ColumnPositionMappingStrategy<TopPosFile> mapper = new ColumnPositionMappingStrategy<>(); // 映射的实体类
mapper.setType(TopPosFile.class);
CsvToBean<TopPosFile> csvToBean = new CsvToBean<>();
//CSVToBeanFilter csvToBeanFilter = new CSVToBeanFilter(); //过滤器 过滤特定行的数据
csvToBean.setCsvReader(reader);
//csvToBean.setFilter(csvToBeanFilter);
csvToBean.setMappingStrategy(mapper);
Iterator<TopPosFile> iterator = csvToBean.iterator();
topPosFileService.clearFileData();
while (iterator.hasNext()){
TopPosFile topPosFile = iterator.next();
topPosFileService.insertSelective(topPosFile);
}
log.info("pos对账文件:{} 落临时库结束",fileName);
}else {
log.info("pos对账文件:{} 不能为空,请确认",fileName);
}
}catch (Exception e){
log.info("pos对账文件:{} 落临时库异常结束",e);
throw new BizException("pos对账文件:{} 落临时库异常结束,错误信息:{}",e.getMessage());
}
}
第四步:必要时编写过滤器 (在此注释掉是因为没用到过滤器)
过滤器要实现CsvToBeanFilter这个接口的allowLine(String[] line)方法,具体代码如下:
public class CSVToBeanFilter implements CsvToBeanFilter {
String startStr = "000000000";
String endStr = "999999999";
//过滤以000000000开始或者以999999999结束的数据
@Override
public boolean allowLine(String[] line) {
if(startStr.equals(line[0]) || endStr.equals(line[0])){
return false;
}
return true;
}
}
解析后的结果:
至此,一个csv文件解析并入库完成。。。。。。。。。。。。。。。。