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中逐行比較兩個不同文字檔案的內容。