Java程序读取大型文本文件逐行的内容

2023年 8月 28日 67.3k 0

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) {

// 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:javajavaprgstufilehandlingFile Handling TutorialCode1_ReadFiles>javac Read.java
C:javajavaprgstufilehandlingFile Handling TutorialCode1_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 实例。一个构造函数接受 String 作为参数,另一个构造函数接受文件对象作为参数。

示例(方法 2)

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:javajavaprgstufilehandlingFile Handling TutorialCode1_ReadFilessample.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:javajavaprgstufilehandlingFile Handling TutorialCode1_ReadFiles>javac Read2.java
C:javajavaprgstufilehandlingFile Handling TutorialCode1_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){

//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:javajavaprgstufilehandlingFile Handling TutorialCode1_ReadFiles>javac Read3.java
C:javajavaprgstufilehandlingFile Handling TutorialCode1_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程序读取大型文本文件逐行的内容的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论