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

我怎样才能快速做到呢?


当前回答

我记录并测试了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种文件读取方法的完整排名。

其他回答

看看这个博客:

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

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

我记录并测试了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种文件读取方法的完整排名。

Java 9:

try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
    stream.forEach(System.out::println);
}

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

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