在Java中逐行比较两个不同的文件
在本文中,我们将比较系统中保存的两个不同的文本文件。我们会逐行检查每个文本文件,通过比较我们可以找出异同。
让我们来看看如何使用Java编程语言来实现它。
向您展示一些实例
实例1
下图描绘了两个具有相同内容的不同文本文件,因此输出将是两个具有相同内容的文件。
实例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()方法读取两个文件的内容并进行比较。
<span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">BufferedReader</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">FileReader</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Main</span><span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span><span class="token punctuation">{</span> <span class="token class-name">BufferedReader</span> reader1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedReader</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileReader</span><span class="token punctuation">(</span><span class="token string">"E:\file1.txt"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">BufferedReader</span> reader2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedReader</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileReader</span><span class="token punctuation">(</span><span class="token string">"E:\file2.txt"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">String</span> line1 <span class="token operator">=</span> reader1<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">String</span> line2 <span class="token operator">=</span> reader2<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> lineNum <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">boolean</span> areEqual <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>line1 <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">||</span> line2 <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>line1 <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> line2 <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span> areEqual <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span> line1<span class="token punctuation">.</span><span class="token function">equalsIgnoreCase</span><span class="token punctuation">(</span>line2<span class="token punctuation">)</span><span class="token punctuation">) </span><span class="token punctuation">{</span> areEqual <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> line1 <span class="token operator">=</span> reader1<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> line2 <span class="token operator">=</span> reader2<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> lineNum<span class="token operator">++</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span><span class="token punctuation">(</span>areEqual<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Both the files have same content"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}<font color="#000000"> </font></span><span class="token keyword">else </span><span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Both the files have different content"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"In both files, there is a difference at line number: "</span><span class="token operator">+</span>lineNum<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"One file has "</span><span class="token operator">+</span>line1<span class="token operator">+</span><span class="token string">" and another file has "</span><span class="token operator">+</span>line2<span class="token operator">+</span><span class="token string">" at line "</span><span class="token operator">+</span>lineNum<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> reader1<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> reader2<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
输出
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:使用内存映射文件
示例
在这种方法中,我们将利用内存映射文件的概念,这是一个将磁盘文件的字节映射到系统内存地址的内核对象,通过操作内存映射文件的内容,我们可以知道内容是相同还是不同。
<span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">File</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>nio<span class="token punctuation">.</span>file<span class="token punctuation">.</span></span><span class="token class-name">Path</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>nio<span class="token punctuation">.</span>file<span class="token punctuation">.</span></span><span class="token class-name">Paths</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">IOException</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">RandomAccessFile</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>nio<span class="token punctuation">.</span></span><span class="token class-name">MappedByteBuffer</span><span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>nio<span class="token punctuation">.</span>channels<span class="token punctuation">.</span></span><span class="token class-name">FileChannel</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Main</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">Path</span> path1 <span class="token operator">=</span> <span class="token class-name">Paths</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"E://file1.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Path</span> path2 <span class="token operator">=</span> <span class="token class-name">Paths</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"E://file2.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">compare</span><span class="token punctuation">(</span>path1<span class="token punctuation">,</span>path2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token class-name">Path</span> path1<span class="token punctuation">,</span> <span class="token class-name">Path</span> path2<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token class-name">RandomAccessFile</span> randomAccessFile1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RandomAccessFile</span><span class="token punctuation">(</span>path1<span class="token punctuation">.</span><span class="token function">toFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"r"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">RandomAccessFile</span> randomAccessFile2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RandomAccessFile</span><span class="token punctuation">(</span>path2<span class="token punctuation">.</span><span class="token function">toFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"r"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">FileChannel</span> ch1 <span class="token operator">=</span> randomAccessFile1<span class="token punctuation">.</span><span class="token function">getChannel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">FileChannel</span> ch2 <span class="token operator">=</span> randomAccessFile2<span class="token punctuation">.</span><span class="token function">getChannel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>ch1<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> ch2<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Both files have different content"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">long</span> size <span class="token operator">=</span> ch1<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">MappedByteBuffer</span> m1 <span class="token operator">=</span> ch1<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token class-name">FileChannel<span class="token punctuation">.</span>MapMode</span><span class="token punctuation">.</span>READ_ONLY<span class="token punctuation">,</span> <span class="token number">0L</span><span class="token punctuation">,</span> size<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">MappedByteBuffer</span> m2 <span class="token operator">=</span> ch2<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token class-name">FileChannel<span class="token punctuation">.</span>MapMode</span><span class="token punctuation">.</span>READ_ONLY<span class="token punctuation">,</span> <span class="token number">0L</span><span class="token punctuation">,</span> size<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>m1<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>m2<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Both files have same content"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span><span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
输出
Both files have same content
注意 - 这里我们考虑过的两个文件,它们具有相同的内容。
在本文中,我们探讨了如何在 Java 中逐行比较两个不同文本文件的内容。
以上就是在Java中逐行比较两个不同的文件的详细内容,更多请关注其它相关文章!