Java程式逐行讀取大型文字檔案
本文介紹了在Java中逐行讀取文字檔案的方法。這裡解釋了三種不同的方法並附帶示例。特別是當資料從一種結構化/非結構化形式傳輸到另一種形式時,將檔案儲存為文字格式非常重要。因此,基本的 檔案傳輸系統將其應用程式元件整合到txt檔案的讀取和寫入過程中。因此,瞭解如何使用文字檔案讀取和寫入方法對於建立解析器也很重要。
多種方法
給定的問題陳述透過三種不同的方法解決:
讓我們按順序檢視程式及其輸出。
注意 - 這些程式在任何線上Java編譯器上都不會產生預期的結果。線上編輯器無法訪問您本地系統的檔案路徑。
使用BufferedReader
在這種方法中,緩衝區由BufferedReader提供給FileReader。它的效能很高效,因為它使用大塊讀取而不是一次讀取單個字元。
-
指定要讀取的檔案。
-
根據不同的方法讀取檔案行,這意味著檔案被逐塊讀取。
-
在顯示屏/螢幕上列印從檔案中讀取的行。
示例
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Read {
public static void main(String[] args) {
// making the BufferedReader object
BufferedReader b_reader;
try {
// Reading the sample.txt file
b_reader = new BufferedReader(new FileReader("sample.txt"));
//reading line by line
String ln = b_reader.readLine();
while (ln != null) {
//printing the line
System.out.println(ln);
ln = b_reader.readLine();
}
b_reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
輸出
C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>javac Read.java C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>java Read Block1 This is an experimental file. Inserting the line 100 This is an experimental file. Inserting the line 101 This is an experimental file. Inserting the line 102 ….. This is an experimental file. Inserting the line 277 Block2 This is an experimental file. Inserting the line 100 This is an experimental file. Inserting the line 101 …. This is an experimental file. Inserting the line 277 Block3 This is an experimental file. Inserting the line 100 … This is an experimental file. Inserting the line 277
使用的示例檔案包含3個包含177行的塊。
解釋
BufferReader與BufferedInputStream不同,因為BufferedReader讀取字元,而BufferedInputStream讀取原始位元組。Reader是BufferedReader的超類,Reader類的超類是Object類。BufferedReader類支援兩個建構函式。使用這些建構函式建立bufferedReader例項。一個建構函式接受reader例項,另一個建構函式接受reader例項和緩衝區大小。
使用FileInputStream
Java應用程式可以使用FileInputStream從文字檔案讀取資料。在這種方法中,檔案內容作為位元組流讀取。
-
指定要讀取的檔案。
-
檢索檔案行,檔案作為位元組讀取。
-
在螢幕上顯示從檔案中讀取的行。
示例
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class Read2 {
public static void main(String[] args)
// exception handling
throws FileNotFoundException{
String path = "C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles\sample.txt";
//passing the file as a parameter to FileInputStream
InputStream ins = new FileInputStream(path);
// using Scanner scn to read the file input stream
try (Scanner scn = new Scanner(
ins, StandardCharsets.UTF_8.name())) {
while (scn.hasNextLine()) {
//printing line by line
System.out.println(scn.nextLine());
}
}
}
}
輸出
C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>javac Read2.java C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>java Read2 Block1 This is an experimental file. Inserting the line 100 This is an experimental file. Inserting the line 101 This is an experimental file. Inserting the line 102 This is an experimental file. Inserting the line 103 This is an experimental file. Inserting the line 104 This is an experimental file. Inserting the line 105 This is an experimental file. Inserting the line 106 ……………………………………
使用的示例檔案包含3個包含177行的塊。
解釋
InputStream是FileInputStream的超類。FileInputStream類支援三個建構函式。使用這些建構函式建立FileInputStream例項。一個建構函式接受String作為引數,另一個建構函式接受檔案物件作為引數。
使用FileReader
Java應用程式可以使用FileReader從文字檔案讀取資料。在這種情況下,檔案的內容作為字元流讀取。
-
設定檔案的路徑。
-
讀取檔案行,檔案作為字元讀取。
-
在顯示屏/螢幕上顯示從檔案中讀取的行。
示例
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Read3{
public static void main(String[] args){
//Set the name of the file to read
File fileone = new File("sample.txt");
// make frd as a FileReader object
try (FileReader frd = new FileReader(fileone)){
int content1;
while ((content1 = frd.read()) != -1) {
System.out.print((char) content1);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
輸出
C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>javac Read3.java C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles>java Read3 Block1 This is an experimental file. Inserting the line 100 This is an experimental file. Inserting the line 101 This is an experimental file. Inserting the line 102 This is an experimental file. Inserting the line 103 This is an experimental file. Inserting the line 104 This is an experimental file. Inserting the line 105 This is an experimental file. Inserting the line 106 This is an experimental file. Inserting the line 107 ……………………………………….
使用的示例檔案包含3個包含177行的塊。
解釋
Reader是FileReader的超類,Reader類的超類是Object類。指定檔案的正確路徑以讀取大型文字。
結論
在以上文章中,使用了三種不同的方法來使用Java語言從檔案中讀取行。第一種方法將行作為字元塊讀取。另一種方法將行作為位元組流讀取,第三種方法將行作為字元讀取。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP