java8优雅的读取本地txt文件转换成List<对象> 输出

2023-05-11  本文已影响0人  IT男的假智慧

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();

}

上一篇 下一篇

猜你喜欢

热点阅读