逐行读取大型文本文件的 Java 程序
本文介绍了逐行读取文本文件的方法。这里通过示例解释了三种不同的方法。以文本格式存储文件非常重要,尤其是当数据从一种结构化/非结构化形式传输到另一种形式时。因此,基本文件传输系统将 txt 文件读取和写入过程集成为其应用程序组件。因此,如何使用文本文件读写方法对于制作解析器也很重要。
多种方法
给定的问题陈述可以用三种不同的方法解决
通过使用 BufferedReader 类
通过使用 FileInputStream
通过使用 FileReader
让我们按顺序查看程序及其输出。
注意 - 这些程序不会在任何在线 Java 编译器上给出预期的结果。因为在线编辑器不会访问您本地系统的文件路径。
方法 1:- 通过使用 BufferedReader
在这种方法中,缓冲区由 BufferedReader 提供给 FileReader。它的性能非常有效,因为它使用更大的块读取而不是一次读取单个字符。
算法
步骤 1 - 指定要读取的文件。
步骤 2 - 根据不同的方法读取文件行,这意味着文件被读取为一个块。
步骤 3 - 在显示器/屏幕上打印从文件读取的行。
解释
BufferReader 与 BufferedInputStream 不同,因为 BufferedReader 读取字符,而 BufferedInputStream 读取原始字节。Reader 是 BufferedReader 的超类,Reader 类的超类是 Object 类。BufferedReader 类支持两个构造函数。使用这些构造函数创建 bufferedReader 实例。一个构造函数接受读取器实例,另一个构造函数接受读取器实例和缓冲区大小。
示例(方法 1)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Read {
public static void main(String[] args) {
// 创建 BufferedReader 对象
BufferedReader b_reader;
try {
// 读取sample.txt文件
b_reader = new BufferedReader(new FileReader("sample.txt"));
//逐行读取
String ln = b_reader.readLine();
while (ln != null) {
//打印该行
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 行
方法 2:- 使用 FileInputStream
Java 应用程序可以使用 FileInputStream 从文本文件中读取数据。在此方法中,文件内容将作为字节流读取。
算法
步骤 1 - 指定要读取的文件。
步骤 2 - 检索文件行,并将文件作为字节读取。
步骤 3 - 在屏幕上显示从文件读取的行。
说明
InputStream 是 FileInputStream 的超类。
FileInputStream 类支持三个构造函数。使用这些构造函数可以创建 FileInputStream 实例。一个构造函数接受字符串作为参数,另一个构造函数接受文件对象作为参数。
示例(方法 2)
s
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)
// 异常处理
throws FileNotFoundException{
String path = "C:\java\javaprgstu\filehandling\File Handling Tutorial\Code\1_ReadFiles\sample.txt";
//将文件作为参数传递给 FileInputStream
InputStream ins = new FileInputStream(path);
// 使用 Scanner scn 读取文件输入流
try (Scanner scn = new Scanner(
ins, StandardCharsets.UTF_8.name())) {
while (scn.hasNextLine()) {
//逐行打印
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 行
方法 3:- 使用 FileReader。
Java 应用程序可以使用 FileReader 从文本文件中读取数据。在这种情况下,文件的内容被读取为字符流。
算法
步骤 1 - 设置文件路径。
步骤 2 - 读取文件行,文件被读取为字符。
步骤 3 - 在显示器/屏幕上显示从文件读取的行。
解释
reader 是 FileReader 的超类,Reader 类的超类是 Object 类。指定文件的正确路径以读取大文本。
示例(方法 3)
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Read3{
public static void main(String[] args){
//设置要读取的文件的名称
File fileone = new File("sample.txt");
// 将 frd 作为 FileReader 对象
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 行
结论
在上面的文章中,使用 Java 语言从文件中读取行使用了三种不同的方法。第一种方法将行读取为字符块。另一种方法将行读取为字节流,第三种方法将行读取为字符。
相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

