[英]How can I read a large text file line by line using Java?
我需要使用 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
泄漏的 scope。
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 很久以后才會關閉底層文件句柄。
看看這個博客:
可以指定緩沖區大小,也可以使用默認大小。 對於大多數用途,默認值足夠大。
// 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 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);
}
這是一個包含完整錯誤處理並支持 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;
}
}
我記錄並測試了 10 種不同的方法來讀取 Java 中的文件,然后通過使它們從 1KB 到 1GB 的測試文件中讀取來相互運行它們。 以下是讀取 1GB 測試文件最快的 3 種文件讀取方法。
請注意,在運行性能測試時,我沒有對控制台進行任何 output 任何操作,因為這確實會減慢測試速度。 我只是想測試原始閱讀速度。
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 種文件閱讀方法的完整排名。
在 Java 8 中,您可以執行以下操作:
try (Stream<String> lines = Files.lines (file, StandardCharsets.UTF_8))
{
for (String line : (Iterable<String>) lines::iterator)
{
;
}
}
一些注意事項: Files.lines 返回的Files.lines
(與大多數流不同)需要關閉。 由於這里提到的原因,我避免使用forEach()
。 奇怪的代碼(Iterable<String>) lines::iterator
將 Stream 轉換為 Iterable。
您可以做的是使用 Scanner 和 go 逐行掃描整個文本。 當然,您應該導入以下內容:
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()
function 是一個 boolean 如果文本中還有更多行則返回 true。 .nextLine()
function 為您提供一整行作為字符串,然后您可以按照您想要的方式使用它。 嘗試System.out.println(line)
打印文本。
旁注:.txt 是文件類型文本。
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);
}
在 Java 8 中,還有使用Files.lines()
的替代方法。 如果您的輸入源不是文件,而是更抽象的東西,例如Reader
或InputStream
,您可以通過BufferedReader
的lines()
方法stream行。
例如:
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();
}
}
}
您可以使用掃描儀 class
Scanner sc=new Scanner(file);
sc.nextLine();
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.forEach(System.out::println);
}
您需要在class BufferedReader
中使用readLine()
方法。 從那個 class 創建一個新的 object 並在他身上運行這個方法並將它保存到一個字符串。
實現這一目標的明確方法,
例如:
如果您的當前目錄中有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();
}
}
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) {
}
}
}
By using the org.apache.commons.io package, it gave more performance, especially in legacy code which uses Java 6 and below.
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();
}
}
}
您可以逐行讀取文件數據,如下所示:
String fileLoc = "fileLocationInTheDisk";
List<String> lines = Files.lines(Path.of(fileLoc), StandardCharsets.UTF_8).collect(Collectors.toList());
對於最終來到這里的 Android 開發者(使用Kotlin的人):
val myFileUrl = object{}.javaClass.getResource("/vegetables.txt")
val file = File(myFileUrl.toURI())
file
.bufferedReader()
.lineSequence()
.forEach(::println)
要么:
val myFileUrl = object{}.javaClass.getResource("/vegetables.txt")
val file = File(myFileUrl.toURI())
file.useLines { lines ->
lines.forEach(::println)
}
vegetables.txt文件應該在你的類路徑中(例如,在src/main/resources目錄中)
上述解決方案都默認將文件編碼視為UTF-8
。 您可以將所需的編碼指定為函數的參數。
上述解決方案不需要任何進一步的操作,例如關閉文件或閱讀器。 它們由 Kotlin 標准庫自動處理。
您還可以使用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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.