共計 784 個字符,預(yù)計需要花費 2 分鐘才能閱讀完成。
在 Java 中,你可以使用以下步驟來對比兩個 txt 文本文檔的數(shù)據(jù):
- 使用
BufferedReader
讀取第一個 txt 文本的內(nèi)容,并將其保存到一個ArrayList
或HashSet
等數(shù)據(jù)結(jié)構(gòu)中。
BufferedReader reader1 = new BufferedReader(new FileReader("file1.txt"));
List<String> data1 = new ArrayList<>();
String line1;
while ((line1 = reader1.readLine()) != null) {data1.add(line1);
}
reader1.close();
- 使用
BufferedReader
讀取第二個 txt 文本的內(nèi)容,并將其保存到另一個ArrayList
或HashSet
中。
BufferedReader reader2 = new BufferedReader(new FileReader("file2.txt"));
List<String> data2 = new ArrayList<>();
String line2;
while ((line2 = reader2.readLine()) != null) {data2.add(line2);
}
reader2.close();
- 使用
equals()
方法逐行對比兩個數(shù)據(jù)集。
boolean areEqual = data1.equals(data2);
if (areEqual) {System.out.println("The content of the files is identical.");
} else {System.out.println("The content of the files is not identical.");
}
注意:如果你需要按行對比而不是整個文本文件,請將每一行的內(nèi)容保存到數(shù)據(jù)結(jié)構(gòu)中,然后逐行對比。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完