在Java中逐行比较两个不同的文件

2023年 8月 28日 64.4k 0

在本文中,我们将比较系统中保存的两个不同的文本文件。我们会逐行检查每个文本文件,通过比较我们可以找出异同。

让我们来看看如何使用Java编程语言来实现它。

向您展示一些实例

实例1

下图描绘了两个具有相同内容的不同文本文件,因此输出将是两个具有相同内容的文件。

在Java中逐行比较两个不同的文件

实例2

下面表示两个文件,假设是file1.txt和file2.txt,以及它们的内容。

文件1.txt

This is amazing.
Java Language.

登录后复制

file2.txt

This is amazing.
Python Language.

登录后复制

在这里,我们可以注意到两个文件在第 2 行有不同的内容。由于文件 1,第 2 行包含“Java 语言”,文件 2,第 2 行包含“Python 语言”

算法

  • 步骤 1 − 创建reader1和reader2作为两个BufferedReader对象,并使用它们逐行读取两个输入文本文件。

  • 第 2 步 - 创建两个变量。首先,创建一个名为“areEqual”的布尔变量并将其初始化为 true。其次,创建一个名为“lineNum”的 int 变量并将其初始化为 1。areEqual 是一个标志变量,最初设置为 true,当输入文件的内容不同时更改为 false。行数将保存在 lineNum 中。

  • 步骤 3 - 将文件 1 的内容读入第 1 行,将文件 2 的内容读入第 2 行。

  • 第四步 - 将文件file1和file2中的行分别读入line1和line2,直到两个文件都被读取完毕。如果line1或line2为空,则将"areEqual"设置为false。

  • 步骤 5 − 如果 areEqual 为真,则声明两个文件的内容相同。如果 areEqual 的值为假,则声明文件的内容不同。

  • 第 6 步 - 关闭资源。

多种方法

我们通过不同的方式提供了解决方案。

  • 通过使用 BufferedReader 类

  • 通过使用内存映射文件

逐一查看程序及其输出。

方法 1:使用 BufferedReader 类

示例

在这种方法中,您将创建BufferedReader类的对象,并使用内置的readLine()方法读取两个文件的内容并进行比较。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main{
public static void main(String[] args) throws IOException{

BufferedReader reader1 = new BufferedReader(new FileReader("E:file1.txt"));
BufferedReader reader2 = new BufferedReader(new FileReader("E:file2.txt"));
String line1 = reader1.readLine();
String line2 = reader2.readLine();
int lineNum = 1;
boolean areEqual = true;
while (line1 != null || line2 != null){
if(line1 == null || line2 == null){
areEqual = false;
break;
} else if(! line1.equalsIgnoreCase(line2)) {
areEqual = false;
break;
}
line1 = reader1.readLine();
line2 = reader2.readLine();
lineNum++;
}
if(areEqual){
System.out.println("Both the files have same content");
} else {
System.out.println("Both the files have different content");
System.out.println("In both files, there is a difference at line number: "+lineNum);
System.out.println("One file has "+line1+" and another file has "+line2+" at line "+lineNum);
}
reader1.close();
reader2.close();
}
}

登录后复制

输出

Both the files have different content
In both files, there is a difference at line number: 2
One file has Java Language. and another file has Python Language. at line 2

登录后复制

注意 - 这里的输入场景类似于上面解释的实例2。

方法 2:使用内存映射文件

示例

在这种方法中,我们将利用内存映射文件的概念,这是一个将磁盘文件的字节映射到系统内存地址的内核对象,通过操作内存映射文件的内容,我们可以知道内容是相同还是不同。

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Main {
public static void main(String[] args) {
Path path1 = Paths.get("E://file1.txt");
Path path2 = Paths.get("E://file2.txt");
compare(path1,path2);
}
public static void compare(Path path1, Path path2) {
try {
RandomAccessFile randomAccessFile1 = new RandomAccessFile(path1.toFile(), "r");
RandomAccessFile randomAccessFile2 = new RandomAccessFile(path2.toFile(), "r");
FileChannel ch1 = randomAccessFile1.getChannel();
FileChannel ch2 = randomAccessFile2.getChannel();
if (ch1.size() != ch2.size()) {
System.out.println("Both files have different content");
}
long size = ch1.size();
MappedByteBuffer m1 = ch1.map(FileChannel.MapMode.READ_ONLY, 0L, size);
MappedByteBuffer m2 = ch2.map(FileChannel.MapMode.READ_ONLY, 0L, size);
if (m1.equals(m2)) {
System.out.println("Both files have same content");
}
}
catch(Exception e){
System.out.println(e);
}
}
}

登录后复制

输出

Both files have same content

登录后复制

注意 - 这里我们考虑过的两个文件,它们具有相同的内容。

在本文中,我们探讨了如何在 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中的所有评论

发布评论