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

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

问题描述

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

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

如何快速执行此操作?

推荐答案

一种常见的模式是使用

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

注意:您必须将Stream放在 try-with-资源块,以确保在其上调用#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天全站免登陆