import java.io.*;
import java.util.*;
public class RemoveDuplicates {
public static void main(String[] args) throws Exception {
// 打开第一个文件并读取数据到set1中
BufferedReader reader1 = new BufferedReader(new FileReader("file1.txt"));
Set<String> set1 = new HashSet<String>();
String line = reader1.readLine();
while (line != null) {
set1.add(line);
line = reader1.readLine();
}
reader1.close();
// 打开第二个文件并读取数据到set2中
BufferedReader reader2 = new BufferedReader(new FileReader("file2.txt"));
Set<String> set2 = new HashSet<String>();
line = reader2.readLine();
while (line != null) {
set2.add(line);
line = reader2.readLine();
}
reader2.close();
// 将两个set合并
set1.addAll(set2);
// 打开输出文件并写入去重后的数据
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
for (String str : set1) {
writer.write(str);
writer.newLine();
}
writer.flush();
writer.close();
}
}
如果你需要保持输入输出文件中的顺序不变,可以使用LinkedHashSet来代替HashSet。只需要将上面示例代码中的HashSet改为LinkedHashSet即可。