java处理微信对账单工具类
时间: 2023-10-18 22:10:42 浏览: 69
以下是一个简单的 Java 工具类,可以用于解析微信支付的对账单文件:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class WeChatBillParser {
private static final String FIELD_SEPARATOR = ",";
public static List<WeChatBillItem> parse(String filePath) throws IOException {
List<WeChatBillItem> items = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
boolean isHeader = true;
while ((line = reader.readLine()) != null) {
if (isHeader) {
isHeader = false;
continue;
}
String[] fields = line.split(FIELD_SEPARATOR);
if (fields.length != 24) {
throw new IllegalArgumentException("Invalid number of fields in line");
}
WeChatBillItem item = new WeChatBillItem();
item.setTransactionTime(fields[0]);
item.setAppId(fields[1]);
item.setMchId(fields[2]);
item.setSubMchId(fields[3]);
item.setDeviceInfo(fields[4]);
item.setTransactionId(fields[5]);
item.setOutTradeNo(fields[6]);
item.setOpenId(fields[7]);
item.setTradeType(fields[8]);
item.setTradeStatus(fields[9]);
item.setBankType(fields[10]);
item.setFeeType(fields[11]);
item.setTotalFee(fields[12]);
item.setRedPacketFee(fields[13]);
item.setRefundId(fields[14]);
item.setOutRefundNo(fields[15]);
item.setRefundFee(fields[16]);
item.setRedPacketRefundFee(fields[17]);
item.setRefundType(fields[18]);
item.setTransactionName(fields[19]);
item.setAttach(fields[20]);
item.setServiceCharge(fields[21]);
item.setRate(fields[22]);
item.setCreateTime(fields[23]);
items.add(item);
}
reader.close();
return items;
}
}
```
其中,`WeChatBillItem` 是一个简单的 POJO 类,用于存储对账单的每一行数据:
```java
public class WeChatBillItem {
private String transactionTime;
private String appId;
private String mchId;
private String subMchId;
private String deviceInfo;
private String transactionId;
private String outTradeNo;
private String openId;
private String tradeType;
private String tradeStatus;
private String bankType;
private String feeType;
private String totalFee;
private String redPacketFee;
private String refundId;
private String outRefundNo;
private String refundFee;
private String redPacketRefundFee;
private String refundType;
private String transactionName;
private String attach;
private String serviceCharge;
private String rate;
private String createTime;
// getters and setters
}
```
使用方法:
```java
String filePath = "path/to/wechat/bill.csv";
List<WeChatBillItem> items = WeChatBillParser.parse(filePath);
for (WeChatBillItem item : items) {
// do something with each item
}
```
阅读全文