如何使用Java逐行读取大型文本文件? [英] How to read a large text file line by line using Java?
问题描述
我需要使用Java逐行读取大约5-6 GB的大文本文件。
我该如何快速执行此操作?
常见模式是使用
try(BufferedReader br = new BufferedReader(new FileReader(file))){
String line; ((line = br.readLine())!= null){
//处理行。
你可以更快地读取数据,没有字符编码。例如ASCII-7,但不会有太大的区别。编辑:不常用的模式,避免使用的范围,
$ b try(BufferedReader br = new BufferedReader(new FileReader(file))){
for(String line;(line = br.readLine())!= null;){
//处理该行。
}
//在这里不可见。
$ p $更新:在Java 8中你可以这样做:
$ $ p $ try(Stream< String> stream = Files.lines(Paths.get(fileName))){
stream.forEach(System.out :: println);
$ / code>
注意:您必须将Stream放在try-with-resource块以确保调用#close方法,否则基本的文件句柄将永远不会关闭,直到GC晚了很久。
I need to read a large text file of around 5-6 GB line by line using Java.
How can I do this quickly?
解决方案 A common pattern is to use
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
}
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.
EDIT: 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.
}
UPDATE: In Java 8 you can do
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.forEach(System.out::println);
}
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屋!