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

我怎样才能快速做到呢?


当前回答

你所能做的就是使用扫描仪扫描整个文本,并逐行浏览文本。 当然,您应该导入以下内容:

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是文件类型文本。

其他回答

使用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();
        }
    }
}

我的阅读习惯通常很简单:

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

Maven

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

我记录并测试了10种不同的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中测试成功,但在Java 7中不能工作,因为缺少对lambda表达式的支持。读取一个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) BufferedReader

测试在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种文件读取方法的完整排名。

一种常见的模式是使用

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在很久之后才关闭。