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

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

作者: IT男的假智慧 | 来源:发表于2023-05-11 15:56 被阅读0次

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<对象> 输出

      本文链接:https://www.haomeiwen.com/subject/npmssdtx.html