man*_*ngh 813 java io performance file-io garbage-collection
我需要使用Java逐行读取大约5-6 GB的大文本文件.
我该怎么办?
Pet*_*rey 1028
一种常见的模式是使用
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
}
Run Code Online (Sandbox Code Playgroud)
如果假设没有字符编码,则可以更快地读取数据.例如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.
}
Run Code Online (Sandbox Code Playgroud)
更新:在Java 8中你可以做到
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
注意:您必须将Stream放在try-with-resource块中以确保在其上调用#close方法,否则在GC稍后执行它之前,基础文件句柄永远不会关闭.
Nav*_*eed 148
看看这个博客:
可以指定缓冲区大小,或者可以使用默认大小.对于大多数用途,默认值足够大.
// 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();
Run Code Online (Sandbox Code Playgroud)
msa*_*yag 90
一旦java-8退出(2014年3月),您将能够使用流:
try (Stream<String> lines = Files.lines(Paths.get(filename), Charset.defaultCharset())) {
lines.forEachOrdered(line -> process(line));
}
Run Code Online (Sandbox Code Playgroud)
打印文件中的所有行:
try (Stream<String> lines = Files.lines(file, Charset.defaultCharset())) {
lines.forEachOrdered(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*tar 37
下面是一个完整的错误处理和支持Java 7之前的charset规范的示例.使用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 */ } }
}
Run Code Online (Sandbox Code Playgroud)
这是Groovy版本,具有完整的错误处理:
File f = new File("textfile.txt");
f.withReader("UTF-8") { br ->
br.eachLine { line ->
println line;
}
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*sky 20
在Java 8中,您可以:
try (Stream<String> lines = Files.lines (file, StandardCharsets.UTF_8))
{
for (String line : (Iterable<String>) lines::iterator)
{
;
}
}
Run Code Online (Sandbox Code Playgroud)
一些注意事项:Files.lines需要关闭(与大多数流不同)返回的流.由于这里提到的原因,我避免使用forEach().奇怪的代码(Iterable<String>) lines::iterator将Stream转换为Iterable.
小智 19
您可以做的是使用扫描仪扫描整个文本并逐行浏览文本.当然你应该导入以下内容:
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
}
}
Run Code Online (Sandbox Code Playgroud)
扫描仪基本上扫描所有文本.while循环用于遍历整个文本.
该.hasNextLine()函数是一个布尔值,如果文本中还有更多行,则返回true.该.nextLine()函数为您提供整行作为字符串,然后您可以按照您想要的方式使用.尝试System.out.println(line)打印文本.
附注:.txt是文件类型文本.
liv*_*ove 17
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();
}
Run Code Online (Sandbox Code Playgroud)
如果从Windows导入此文件,则它可能具有ANSI编码(Cp1252),因此您必须指定编码.
Die*_*rte 16
在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);
}
Run Code Online (Sandbox Code Playgroud)
gom*_*sha 14
我记录并测试了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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
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);
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到所有10种文件阅读方法的完整排名.
Rüd*_*ann 12
在Java 8中,还有一种替代方法Files.lines().如果您的输入源不是文件,而是像a Reader或an 那样更抽象的东西InputStream,则可以通过s 方法对行进行流式处理.BufferedReaderlines()
例如:
try (BufferedReader reader = new BufferedReader(...)) {
reader.lines().forEach(line -> processLine(line));
}
Run Code Online (Sandbox Code Playgroud)
将调用processLine()每个读取的输入行BufferedReader.
小智 10
对于使用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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用Scanner类
Scanner sc=new Scanner(file);
sc.nextLine();
Run Code Online (Sandbox Code Playgroud)
您需要使用该readLine()方法class BufferedReader.从该类创建一个新对象并在他上面操作此方法并将其保存为字符串.
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
实现这一目标的明确方法,
例如:
如果您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();
}
}
Run Code Online (Sandbox Code Playgroud)