3 回答

TA貢獻1828條經驗 獲得超13個贊
將第一個文件加載到 List<String> 用戶中。
將第二個文件加載到 List<String> users2 中。
使用 apache 公共集合
CollectionUtils.removeAll(Collection<E> users, Collection<?> users2)
這僅在文件大小可以加載到內存中時才有效。否則,它需要另一種方法,例如使用命令行命令對兩個文件進行排序
sort
,并逐行瀏覽兩個文件并決定是否寫入輸出。

TA貢獻1852條經驗 獲得超7個贊
您可以使用 BeyondCompare 來比較兩個 csvs。它將明確識別丟失的用戶以及其他不匹配的數據(如果有)。如果您想以編程方式執行此操作,則可以在將 csv 復制到 bean 列表/映射后創建一個用戶 bean(并重寫 equals 方法來比較用戶名或任何其他您想要的)。

TA貢獻1906條經驗 獲得超10個贊
我看到的最好的方式,
1)使用Java NIO Api(實際上非??欤┓謩e讀取兩個文件并將它們存儲到列表中。
Path path = Paths.get("src/main/resources/shakespeare.txt");
try {
Files.lines(path).forEach(System.out::println);//print each line
} catch (IOException ex) {
ex.printStackTrace();//handle exception here
}
2) 使用 java 8 預測器比較兩個列表。
public static List < String > filterAndGetEmployees(List < String> employees,
Predicate < String > predicate) {
return list.stream().filter(predicate).collect(Collectors. < String > toList());
}
3)如果你想再次寫文件,你可以去,
Path path = Paths.get("src/main/resources/shakespeare.txt");
try(BufferedWriter writer = Files.newBufferedWriter(path, Charset.forName("UTF-8"))){
writer.write("To be, or not to be. That is the question.");
}catch(IOException ex){
ex.printStackTrace();
}
希望對你有幫助..
添加回答
舉報