import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class FileComparison {
public static void main(String[] args) {
Set<String> a = readFileToSet("C:\\Users\\Administrator\\Desktop\\demo\\1.txt");
Set<String> b = readFileToSet("C:\\Users\\Administrator\\Desktop\\demo\\2.txt");
a.removeAll(b);
System.out.println("只在a集合中存在的元素: " + a);
}
private static Set<String> readFileToSet(String fileName) {
Set<String> resultSet = new HashSet<>();
try {
File file = new File(fileName);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
resultSet.add(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("文件未找到: " + fileName);
e.printStackTrace();
}
return resultSet;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36