我需要使用Java逐行读取大约5-6 GB的大型文本文件。
我怎样才能快速做到呢?
我需要使用Java逐行读取大约5-6 GB的大型文本文件。
我怎样才能快速做到呢?
当前回答
一种常见的模式是使用
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
}
如果假设没有字符编码,则可以更快地读取数据。例如,ASCII-7,但它不会有太大的区别。很有可能您对数据的处理将花费更长的时间。
EDIT:一种不太常用的模式,可以避免行泄漏的范围。
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中你可以这样做
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.forEach(System.out::println);
}
注意:你必须将Stream放在try-with-resource块中,以确保对其调用#close方法,否则底层文件句柄永远不会关闭,直到GC在很久之后才关闭。
其他回答
使用Java 8读取文件
package com.java.java8;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
/**
* The Class ReadLargeFile.
*
* @author Ankit Sood Apr 20, 2017
*/
public class ReadLargeFile {
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args) {
try {
Stream<String> stream = Files.lines(Paths.get("C:\\Users\\System\\Desktop\\demoData.txt"));
stream.forEach(System.out::println);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
你可以使用流更精确地做到这一点:
Files.lines(Paths.get("input.txt")).forEach(s -> stringBuffer.append(s);
在Java 7中:
String folderPath = "C:/folderOfMyFile";
Path path = Paths.get(folderPath, "myFileName.csv"); //or any text file eg.: txt, bat, etc
Charset charset = Charset.forName("UTF-8");
try (BufferedReader reader = Files.newBufferedReader(path , charset)) {
while ((line = reader.readLine()) != null ) {
//separate all csv fields into string array
String[] lineVariables = line.split(",");
}
} catch (IOException e) {
System.err.println(e);
}
在Java 8中,你可以这样做:
try (Stream<String> lines = Files.lines (file, StandardCharsets.UTF_8))
{
for (String line : (Iterable<String>) lines::iterator)
{
;
}
}
一些注释:由Files返回的流。行(不像大多数流)需要关闭。由于这里提到的原因,我避免使用forEach()。奇怪的代码(Iterable<String>) lines::iterator将一个Stream转换为一个Iterable。
你可以使用下面的代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadTextFile {
public static void main(String[] args) throws IOException {
try {
File f = new File("src/com/data.txt");
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = "";
System.out.println("Reading file using Buffered Reader");
while ((readLine = b.readLine()) != null) {
System.out.println(readLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}