簡體   English   中英

如何比較兩個文本文件的內容並在另一個文本文件中輸出? 文字1-文字2

[英]How to compare the content of two text files and output in another text file? text1 - text2

我想做1.txt減去2.txt的內容,並在3.txt中輸出。 同樣,如果2.txt還有剩余的行,請在4.txt中輸出。

文本文件的內容不是逐行排序的,因此基本上,代碼僅需要刪除匹配的行,如下所示:

1.txt包含:

example1
example2
example3

2.txt包含:

example2
example3
example4

3.txt將包含:

example1

4.txt將包含:

example4

使用番石榴

Set<String> lines1 = 
    new HashSet<>(Files.readLines(new File("1.txt"), Charsets.UTF_8));
Set<String> lines2 = 
    new HashSet<>(Files.readLines(new File("2.txt"), Charsets.UTF_8));
Set<String> minus1 = Sets.difference(lines1, lines2);
Set<String> minus2 = Sets.difference(lines2, lines1);
Files.asCharSink(new File("3.txt"), Charsets.UTF_8).writeLines(minus1);
Files.asCharSink(new File("4.txt"), Charsets.UTF_8).writeLines(minus2);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM