String keys ="merchantNo,terminalId,tradeTime,refundCompletionTime,tradeAmount,commissionAmount,refundAmount,balanceAmount,tradeType,payMethod,tradeStatus,tradeNo,refundOriginalOrderNo,terminalSerialNumber,channelOrderNo,liquidationDate,userId,bankCardType";
String[] keysArray = keys.split(",");
final int batchSize =1000;
try (Stream lines = Files.lines(Paths.get("D:\\data\\pay\\20230506.txt"))) {
List<ShopMerchantBill> list =new ArrayList<>(batchSize);
lines.map(line -> {
String[] values = line.split(",");
Map map = IntStream.range(0, keysArray.length)
.boxed()
.collect(Collectors.toMap(i -> keysArray[i], i -> values[i]));
ShopMerchantBill bill = JSON.parseObject(JSON.toJSONString(map), ShopMerchantBill.class);
bill.setTradeType(bill.getTradeAmount() >0 ?"退款" :"收款");
return bill;
}).forEach(bill -> {
list.add(bill);
if (list.size() >= batchSize) {
shopMerchantBillService.saveBatch(list);
list.clear();
}
});
if (!list.isEmpty()) {
shopMerchantBillService.saveBatch(list);
}
}catch (IOException e) {
e.printStackTrace();
}
java8优雅的读取本地txt文件转换成List<对象> 输出
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- Spark SQL, DataFrames and Datasets Guide Overview SQL Dat...
- public static void main(String[] args) { String ke...
- Spark SQL, DataFrames and Datasets Guide Overview SQL Dat...