在Java中逐行比较两个不同的文件
在这篇文章中,我们将比较保存在我们系统中的两个不同的文本文件。我们将逐行检查每个文本文件,通过比较这些文件,我们可以确定其相似之处和不同之处。
让我们看看如何通过使用Java编程语言来做到这一点。
给你看一些例子
实例-1
下图描述了两个具有相同内容的不同文本文件,因此输出将是两个具有相同内容的文件。
实例-2
下面表示两个文件,例如file1.txt和file2.txt及其内容。
file1.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 “的布尔变量,并将其初始化为真。第二,创建一个名为 “lineNum “的int变量,并将其初始化为1。areEqual是一个标志变量,最初被设置为真,当输入文件的内容不同时,会被改变为假。行数将被保存在lineNum中。
-
第3步 – 将文件1的内容读入第1行,将文件2读入第2行。
-
第4步 – 继续将文件file1和file2中的行分别读入line1和line2,直到两个文件都被读完。如果line1或line2是空的,将 “areEqual “设置为false。
-
第5步 – 如果areEqual为真,声明两个文件的内容是相同的。如果’areEqual’的值为假,则声明两个文件的内容是不同的。
-
第6步 – 关闭资源。
多种方法
我们已经提供了不同方法的解决方案。
- 通过使用BufferedReader类
-
通过使用内存映射文件
让我们逐一看看这个程序和它的输出。
方法-1:通过使用缓冲读卡器类
示例
在这种方法中,你将创建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逐行比较两个不同文本文件的内容。