简体   繁体   English

如何比较两个文本文件的内容并在另一个文本文件中输出? 文字1-文字2

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

i want to do the content of 1.txt minus 2.txt and output that in 3.txt. 我想做1.txt减去2.txt的内容,并在3.txt中输出。 Also if 2.txt has extra remaining lines, output it in 4.txt. 同样,如果2.txt还有剩余的行,请在4.txt中输出。

The content of the text files are not ordered line by line, so basically the code needs to remove the matching line only, like this: 文本文件的内容不是逐行排序的,因此基本上,代码仅需要删除匹配的行,如下所示:

1.txt contains: 1.txt包含:

example1
example2
example3

2.txt contains: 2.txt包含:

example2
example3
example4

3.txt will contain: 3.txt将包含:

example1

4.txt will contain: 4.txt将包含:

example4

Using Guava : 使用番石榴

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