本文介绍了逐行读取文本文件的方法。这里,通过示例解释这三种不同的方法。以文本格式存储文件非常重要,尤其是当数据从一种结构化/非结构化形式传输到另一种形式时。因此,基本的文件传输系统将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:\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 实例。一个构造函数接受 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:\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 行
方法 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:\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程序读取大型文本文件逐行的内容的详细内容。更多信息请关注PHP中文网其他相关文章!

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runanywhere”哲学。1)itusesbytbytybytecebytecodethatrunsonanyjvm-platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中编写平台特定代码的原因包括访问特定操作系统功能、与特定硬件交互和优化性能。1)使用JNA或JNI访问Windows注册表;2)通过JNI与Linux特定硬件驱动程序交互;3)通过JNI使用Metal优化macOS上的游戏性能。尽管如此,编写平台特定代码会影响代码的可移植性、增加复杂性、可能带来性能开销和安全风险。

Java将通过云原生应用、多平台部署和跨语言互操作进一步提升平台独立性。1)云原生应用将使用GraalVM和Quarkus提升启动速度。2)Java将扩展到嵌入式设备、移动设备和量子计算机。3)通过GraalVM,Java将与Python、JavaScript等语言无缝集成,增强跨语言互操作性。

Java的强类型系统通过类型安全、统一的类型转换和多态性确保了平台独立性。1)类型安全在编译时进行类型检查,避免运行时错误;2)统一的类型转换规则在所有平台上一致;3)多态性和接口机制使代码在不同平台上行为一致。

JNI会破坏Java的平台独立性。1)JNI需要特定平台的本地库,2)本地代码需在目标平台编译和链接,3)不同版本的操作系统或JVM可能需要不同的本地库版本,4)本地代码可能引入安全漏洞或导致程序崩溃。

新兴技术对Java的平台独立性既有威胁也有增强。1)云计算和容器化技术如Docker增强了Java的平台独立性,但需要优化以适应不同云环境。2)WebAssembly通过GraalVM编译Java代码,扩展了其平台独立性,但需与其他语言竞争性能。

不同JVM实现都能提供平台独立性,但表现略有不同。1.OracleHotSpot和OpenJDKJVM在平台独立性上表现相似,但OpenJDK可能需额外配置。2.IBMJ9JVM在特定操作系统上表现优化。3.GraalVM支持多语言,需额外配置。4.AzulZingJVM需特定平台调整。

平台独立性通过在多种操作系统上运行同一套代码,降低开发成本和缩短开发时间。具体表现为:1.减少开发时间,只需维护一套代码;2.降低维护成本,统一测试流程;3.快速迭代和团队协作,简化部署过程。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

记事本++7.3.1
好用且免费的代码编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

WebStorm Mac版
好用的JavaScript开发工具