Java 超大文件(166G)读取

最近有个需求,将超级大的166GB 的csv文件读取解析并入库

废话少说,直接上干货:

走过路过~请点赞

/**
 * @author ff
 * @date 2022/1/6 19:16
 */
@Slf4j
public class FileReader {

    /**
     * 读文件
     *
     * @param filePath     文件路径
     * @param lineConsumer 行处理器
     * @throws IOException
     */
    public static void read(String filePath, Consumer<String> lineConsumer) throws IOException {
        final File csvFile = new File(filePath);
        if (!csvFile.exists()) {
            throw new FileNotFoundException("文件[" + filePath + "]不存在");
        }
        Long num = 0L;
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        try (
                FileInputStream fis = new FileInputStream(csvFile);
                Scanner sc = new Scanner(fis, "UTF-8")
        ) {
            while (sc.hasNext()) {
                String line = sc.nextLine();
                log.info("解析第[{}]行数据[{}]", ++num, line);
                if (Objects.nonNull(lineConsumer)) {
                    lineConsumer.accept(line);
                }
            }
            stopWatch.stop();
            log.info("CSV文件[{}]解析结束, 共用时:[{}]ms", filePath, stopWatch.getTotalTimeMillis());
        } catch (IOException ex) {
            log.error("解析CSV文件[{}]第[{}]行数据发生异常", filePath, num, ex);
        }
    }

    public static void main(String[] args) throws IOException {
        String file = "D:\\迅雷下载\\20220105SYQ.csv\\20220105SYQ.csv";
        FileReader.read(file, (line) -> {
			// 将line进行解析并入库
        });
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值