如何使用 Java 逐行读取大型文本文件? [英] How can I read a large text file line by line using Java?

查看:40
本文介绍了如何使用 Java 逐行读取大型文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 Java 逐行读取大约 5-6 GB 的大型文本文件.

I need to read a large text file of around 5-6 GB line by line using Java.

我怎样才能快速做到这一点?

How can I do this quickly?

推荐答案

一个常见的模式是使用

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
       // process the line.
    }
}

如果您假设没有字符编码,则可以更快地读取数据.例如ASCII-7 但它不会有太大区别.您对数据的处理很可能需要更长的时间.

You can read the data faster if you assume there is no character encoding. e.g. ASCII-7 but it won't make much difference. It is highly likely that what you do with the data will take much longer.

一种不太常用的模式,可避免 line 泄漏的范围.

A less common pattern to use which avoids the scope of line leaking.

try(BufferedReader br = new BufferedReader(new FileReader(file))) {
    for(String line; (line = br.readLine()) != null; ) {
        // process the line.
    }
    // line is not visible here.
}

<小时>

更新:在 Java 8 中你可以做到


UPDATE: In Java 8 you can do

try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
        stream.forEach(System.out::println);
}

注意:您必须将流放在 try-with-resource 块以确保在其上调用 #close 方法,否则底层文件句柄永远不会关闭,直到 GC 很晚才关闭.

NOTE: You have to place the Stream in a try-with-resource block to ensure the #close method is called on it, otherwise the underlying file handle is never closed until GC does it much later.

这篇关于如何使用 Java 逐行读取大型文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆