我需要使用 Java 逐行读取大约 5-6 GB 的大文本文件。
我怎样才能快速做到这一点?
一个常见的模式是使用
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
}
如果假设没有字符编码,则可以更快地读取数据。例如ASCII-7,但不会有太大区别。您对数据的处理很可能需要更长的时间。
编辑:一种不太常见的模式,可以避免 line
泄漏的范围。
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 逐行读取文件 - Java 教程
可以指定缓冲区大小,也可以使用默认大小。对于大多数用途,默认值足够大。
// Open the file
FileInputStream fstream = new FileInputStream("textfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
fstream.close();
DataInputStream
,并且关闭了错误的流。 Java教程没有错,也没有必要像这样随意引用第三方互联网垃圾。
一旦 Java 8 发布(2014 年 3 月),您将能够使用流:
try (Stream<String> lines = Files.lines(Paths.get(filename), Charset.defaultCharset())) {
lines.forEachOrdered(line -> process(line));
}
打印文件中的所有行:
try (Stream<String> lines = Files.lines(file, Charset.defaultCharset())) {
lines.forEachOrdered(System.out::println);
}
StandardCharsets.UTF_8
,使用 Stream<String>
简洁,避免使用 forEach()
,尤其是 forEachOrdered()
,除非有原因。
forEach(this::process)
这样的简短函数引用,这还不错,但是如果您将代码块编写为 forEach()
中的 lambda,它会变得很难看。
forEachOrdered
才能按顺序执行。请注意,在这种情况下您将无法并行化流,尽管我发现除非文件有数千行,否则并行化不会打开。
这是一个示例,其中包含完整的错误处理并支持 Java 7 之前的字符集规范。在 Java 7 中,您可以使用 try-with-resources 语法,这使得代码更清晰。
如果您只想要默认字符集,您可以跳过 InputStream 并使用 FileReader。
InputStream ins = null; // raw byte-stream
Reader r = null; // cooked reader
BufferedReader br = null; // buffered for readLine()
try {
String s;
ins = new FileInputStream("textfile.txt");
r = new InputStreamReader(ins, "UTF-8"); // leave charset out for default
br = new BufferedReader(r);
while ((s = br.readLine()) != null) {
System.out.println(s);
}
}
catch (Exception e)
{
System.err.println(e.getMessage()); // handle exception
}
finally {
if (br != null) { try { br.close(); } catch(Throwable t) { /* ensure close happens */ } }
if (r != null) { try { r.close(); } catch(Throwable t) { /* ensure close happens */ } }
if (ins != null) { try { ins.close(); } catch(Throwable t) { /* ensure close happens */ } }
}
这是带有完整错误处理的 Groovy 版本:
File f = new File("textfile.txt");
f.withReader("UTF-8") { br ->
br.eachLine { line ->
println line;
}
}
ByteArrayInputStream
与读取大文本文件有什么关系?
我记录并测试了 10 different ways to read a file in Java,然后通过让它们读取 1KB 到 1GB 的测试文件来相互运行它们。以下是读取 1GB 测试文件最快的 3 种文件读取方法。
请注意,在运行性能测试时,我没有向控制台输出任何内容,因为这确实会减慢测试速度。我只是想测试原始阅读速度。
1) java.nio.file.Files.readAllBytes()
在 Java 7、8、9 中测试。这是总体上最快的方法。读取 1GB 文件的时间始终不到 1 秒。
import java.io..File;
import java.io.IOException;
import java.nio.file.Files;
public class ReadFile_Files_ReadAllBytes {
public static void main(String [] pArgs) throws IOException {
String fileName = "c:\\temp\\sample-1GB.txt";
File file = new File(fileName);
byte [] fileBytes = Files.readAllBytes(file.toPath());
char singleChar;
for(byte b : fileBytes) {
singleChar = (char) b;
System.out.print(singleChar);
}
}
}
2) java.nio.file.Files.lines()
这已在 Java 8 和 9 中成功测试,但由于缺乏对 lambda 表达式的支持,它在 Java 7 中不起作用。读取一个 1GB 的文件大约需要 3.5 秒,就读取更大的文件而言,它排在第二位。
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.stream.Stream;
public class ReadFile_Files_Lines {
public static void main(String[] pArgs) throws IOException {
String fileName = "c:\\temp\\sample-1GB.txt";
File file = new File(fileName);
try (Stream linesStream = Files.lines(file.toPath())) {
linesStream.forEach(line -> {
System.out.println(line);
});
}
}
}
3) 缓冲读取器
经测试可在 Java 7、8、9 中工作。读取 1GB 测试文件大约需要 4.5 秒。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile_BufferedReader_ReadLine {
public static void main(String [] args) throws IOException {
String fileName = "c:\\temp\\sample-1GB.txt";
FileReader fileReader = new FileReader(fileName);
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
String line;
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}
}
您可以找到所有 10 种文件读取方法的完整排名here。
System.out.print/println()
;您还假设在前两种情况下该文件将适合内存。
在 Java 8 中,您可以执行以下操作:
try (Stream<String> lines = Files.lines (file, StandardCharsets.UTF_8))
{
for (String line : (Iterable<String>) lines::iterator)
{
;
}
}
一些注意事项: Files.lines
返回的流(与大多数流不同)需要关闭。由于 mentioned here 的原因,我避免使用 forEach()
。奇怪的代码 (Iterable<String>) lines::iterator
将 Stream 转换为 Iterable。
Iterable
,这段代码虽然有用,但绝对丑。它需要一个演员表(即(Iterable<String>)
)才能工作。
for(String line : (Iterable<String>) lines.skip(1)::iterator)
Stream
功能,使用 Files.newBufferedReader
而不是 Files.lines
并重复调用 readLine()
直到 null
而不是使用像 (Iterable<String>) lines::iterator
这样的结构似乎要简单得多……
Unlike readAllLines, [File.lines] does not read all lines into a List, but instead populates lazily as the stream is consumed... The returned stream encapsulates a Reader.
您可以做的是使用扫描仪扫描整个文本并逐行浏览文本。当然,您应该导入以下内容:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public static void readText throws FileNotFoundException {
Scanner scan = new Scanner(new File("samplefilename.txt"));
while(scan.hasNextLine()){
String line = scan.nextLine();
//Here you can manipulate the string the way you want
}
}
扫描仪基本上扫描所有文本。 while 循环用于遍历整个文本。
.hasNextLine()
函数是一个布尔值,如果文本中还有更多行,则返回 true。 .nextLine()
函数将整行作为字符串提供给您,然后您可以按照您想要的方式使用它。尝试 System.out.println(line)
打印文本。
旁注:.txt 是文件类型文本。
BufferedReader.readLine()
慢得多,他要求性能最好的方法。
FileReader 不会让您指定编码,如果您需要指定它,请改用 InputStreamReader
:
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "Cp1252"));
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
如果您从 Windows 导入此文件,它可能具有 ANSI 编码 (Cp1252),因此您必须指定编码。
在 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);
}
StandardCharsets.UTF_8
避免 Charset.forName("UTF-8")
中的检查异常
在 Java 8 中,还有一个使用 Files.lines()
的替代方法。如果您的输入源不是文件而是像 Reader
或 InputStream
这样更抽象的文件,您可以通过 BufferedReader
的 lines()
方法流式传输这些行。
例如:
try (BufferedReader reader = new BufferedReader(...)) {
reader.lines().forEach(line -> processLine(line));
}
将为 BufferedReader
读取的每个输入行调用 processLine()
。
用于使用 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();
}
}
}
您可以使用 Scanner 类
Scanner sc=new Scanner(file);
sc.nextLine();
Scanner
很好,但这个答案不包括正确使用它的完整代码。
BufferedReader.readLine()
肯定快几倍。如果您不这么认为,请提供您的理由。
爪哇 9:
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.forEach(System.out::println);
}
System.getProperty("os.name").equals("Linux")
==
进行比较!
您需要使用 class BufferedReader
中的 readLine()
方法。从该类创建一个新对象并对他操作此方法并将其保存为字符串。
实现这一目标的明确方法,
例如:
如果您的当前目录中有 dataFile.txt
import java.io.*;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class readByLine
{
public readByLine() throws FileNotFoundException
{
Scanner linReader = new Scanner(new File("dataFile.txt"));
while (linReader.hasNext())
{
String line = linReader.nextLine();
System.out.println(line);
}
linReader.close();
}
public static void main(String args[]) throws FileNotFoundException
{
new readByLine();
}
}
https://i.stack.imgur.com/W3xLx.jpg
BufferedReader br;
FileInputStream fin;
try {
fin = new FileInputStream(fileName);
br = new BufferedReader(new InputStreamReader(fin));
/*Path pathToFile = Paths.get(fileName);
br = Files.newBufferedReader(pathToFile,StandardCharsets.US_ASCII);*/
String line = br.readLine();
while (line != null) {
String[] attributes = line.split(",");
Movie movie = createMovie(attributes);
movies.add(movie);
line = br.readLine();
}
fin.close();
br.close();
} catch (FileNotFoundException e) {
System.out.println("Your Message");
} catch (IOException e) {
System.out.println("Your Message");
}
这个对我有用。希望它也能帮助你。
您可以使用流更精确地做到这一点:
Files.lines(Paths.get("input.txt")).forEach(s -> stringBuffer.append(s);
我通常会直接进行阅读程序:
void readResource(InputStream source) throws IOException {
BufferedReader stream = null;
try {
stream = new BufferedReader(new InputStreamReader(source));
while (true) {
String line = stream.readLine();
if(line == null) {
break;
}
//process line
System.out.println(line)
}
} finally {
closeQuiet(stream);
}
}
static void closeQuiet(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException ignore) {
}
}
}
通过使用 org.apache.commons.io 包,它提供了更高的性能,尤其是在使用 Java 6 及更低版本的遗留代码中。
Java 7 具有更好的 API,异常处理更少,方法更有用:
LineIterator lineIterator = null;
try {
lineIterator = FileUtils.lineIterator(new File("/home/username/m.log"), "windows-1256"); // The second parameter is optionnal
while (lineIterator.hasNext()) {
String currentLine = lineIterator.next();
// Some operation
}
}
finally {
LineIterator.closeQuietly(lineIterator);
}
马文
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
您可以使用以下代码:
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();
}
}
}
您还可以使用 Apache Commons IO:
File file = new File("/home/user/file.txt");
try {
List<String> lines = FileUtils.readLines(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileUtils.readLines(file)
是一种已弃用的方法。此外,该方法调用 IOUtils.readLines
,它使用 BufferedReader 和 ArrayList。这不是逐行的方法,当然也不是读取几 GB 的实用方法。
您可以逐行读取文件数据,如下所示:
String fileLoc = "fileLocationInTheDisk";
List<String> lines = Files.lines(Path.of(fileLoc), StandardCharsets.UTF_8).collect(Collectors.toList());
OP
要求快速完成,这也没有回答,因为逐行处理会更有效率
不定期副业成功案例分享
for(String line = br.readLine(); line != null; line = br.readLine())
顺便说一句,在 Java 8 中你可以做到try( Stream<String> lines = Files.lines(...) ){ for( String line : (Iterable<String>) lines::iterator ) { ... } }
这很难不让人讨厌。