利用csv将数据写入文件中

欢迎访问个人博客 德鲁大叔撸代码

最近在做一个对账系统,在做完对账业务的时候,需要生成对账单供下游系统使用。
那必然接触到文件的读写,在此我使用csv来做写文件操作。

前提:引入opencsv依赖

1
2
3
4
5
6
<!-- csv依赖 -->
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>4.5</version>
</dependency>

写入文件的方法

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
40
41
42
43
44
45
46
47
48
49
50
51
52
public File writeToDivBill(String fileName,List list)throws IOException{  //list  为需要写入的数据list
String filePath = PyxisConfig.getInstance().getProperty(Constants.CREATE_FILE_PATH);
OutputStreamWriter writer = null;
FileOutputStream fos = null;
CSVWriter csvWriter = null;
try {
List<String[]> tableHeaders =setDivHead(); //设置表头
File directory = new File(filePath);
if(!directory.exists() && !directory.isDirectory()){
directory.mkdir();
}
File divBill = new File(filePath + fileName);
fos = new FileOutputStream(divBill);
writer = new OutputStreamWriter(fos, Charset.forName("GBK"));
csvWriter = new CSVWriter(writer, CSVParser.DEFAULT_SEPARATOR);
log.info("开始写入分账对账单表头,文件名:{}",fileName);
csvWriter.writeAll(tableHeaders);
List<String[]> billDataList = setDivBillBody(list); //写入具体数据
log.info("开始写入分账交易数据,文件名:{}",fileName);
csvWriter.writeAll(billDataList);
log.info("分账交易数据写入完成,文件名:{}",fileName);
return divBill;
}catch (IOException e){
log.error("io error:", e);
throw new IOException(e);
}catch (Exception e){
log.error("writeCSVFile error, error info:{}", e.getMessage());
throw new BizException("writeCSVFile error, error info:{}",e.getMessage());
}finally {
if (csvWriter != null) {
try {
csvWriter.close();
} catch (IOException e) {
log.error("关闭流失败, 失败信息为:{}", e.getMessage());
}
}
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
log.error("关闭流失败, 失败信息为:{}", e.getMessage());
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
log.error("关闭流失败, 失败信息为:{}", e.getMessage());
}
}
}
}

写入表头方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/** 设置分账对账单表头 **/
public static List<String[]> setDivHead() {
List<String[]> fileDataList = new ArrayList<>();
String[] csvHeadText = new String[10];
csvHeadText[0] = "产品号";
csvHeadText[1] = "分账订单号";
csvHeadText[2] = "原交易商户订单号";
csvHeadText[3] = "原交易订单号";
csvHeadText[4] = "原交易订单日期时间";
csvHeadText[5] = "分账类型";
csvHeadText[6] = "交易商户号";
csvHeadText[7] = "交易金额";
csvHeadText[8] = "分账方商户号";
csvHeadText[9] = "分账方入账金额";
fileDataList.add(csvHeadText);
return fileDataList;
}

写入表格数据方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/** 分账对账单数据写入 **/
public List<String[]> setDivBillBody(List<AcctDivDetail> divList) throws Exception{
List<String[]> fileDataList = new ArrayList<>();
for (int i = 0; i < divList.size(); i++) {
String[] divBillBody = new String[10];
divBillBody[0] = divList.get(i).getProductId();
divBillBody[1] = divList.get(i).getAcctDetailId();
divBillBody[2] = divList.get(i).getMerOrdId();
divBillBody[3] = divList.get(i).getHfSeqId();
divBillBody[4] = divList.get(i).getReqDate();
divBillBody[5] = divList.get(i).getIsDelayAcct();
divBillBody[6] = divList.get(i).getPaMerId();
divBillBody[7] = divList.get(i).getOrdAmt().toString();
divBillBody[8] = divList.get(i).getRealCustId();
if(divList.get(i).getDivAmt() == null || divList.get(i).getDivAmt().equals("")){
divBillBody[9] = "0.00";
}else {
divBillBody[9] = divList.get(i).getDivAmt().toString();
}
fileDataList.add(divBillBody);
}
return fileDataList;
}
坚持原创技术分享,您的支持将鼓励我继续创作!