Java中逐行比较两个不同文件
在本文中,我们将比较保存在系统中的两个不同的文本文件。我们将逐行检查每个文本文件,通过比较可以识别出相同点和不同点。
让我们看看如何使用Java编程语言来实现。
展示一些示例
示例1
下图显示了两个内容相同的文本文件,因此输出将是两个内容相同的文本文件。
示例2
下图表示两个文件,例如file1.txt和file2.txt及其内容。
file1.txt
This is amazing. Java Language.
file2.txt
This is amazing. Python Language.
在这里,我们可以注意到这两个文件在第2行有不同的内容。file1的第2行包含“Java Language”,而file2的第2行包含“Python Language”。
算法
步骤1 − 创建reader1和reader2作为两个BufferedReader对象,并使用它们逐行读取两个输入文本文件。
步骤2 − 创建两个变量。首先,创建一个名为“areEqual”的布尔变量并将其初始化为true。其次,创建一个名为“lineNum”的int变量并将其初始化为1。“areEqual”是一个标志变量,最初设置为true,当输入文件的内容不同时更改为false。“lineNum”将保存行号。
步骤3 − 将文件1的内容读入Line 1,将文件2的内容读入Line 2。
步骤4 − 继续将文件file1和file2中的行分别读入line1和line2,直到两个文件都读完。如果line1或line2为空,则将“areEqual”设置为false。
步骤5 − 如果areEqual为true,则声明两个文件的内容相同。如果'areEqual'的值为false,则声明文件的内容不同。
步骤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中逐行比较两个不同文本文件的内容。