找出文件1中有而文件2中没有的数据
生活随笔
收集整理的這篇文章主要介紹了
找出文件1中有而文件2中没有的数据
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
// 使用stream方法實(shí)現(xiàn)public static List<String> fileterData() throws IOException {String file1Content = new String(Files.readAllBytes(Paths.get(".\\src\\main\\resources\\File1")));String file2Content = new String(Files.readAllBytes(Paths.get(".\\src\\main\\resources\\File2")));// String 的 split方法有隱患,WINDOWS下?lián)Q行符使用\r\n,Linux下使用的是\nList<String> file1Str = Arrays.asList(file1Content.split("\r\n"));List<String> file2Str= Arrays.asList(file2Content.split("\r\n"));// 過(guò)濾收集,遍歷file1,每個(gè)都在file2中進(jìn)行檢查一遍,如果file2中沒(méi)有的就收集起來(lái)List<String> filtedStr = file1Str.stream().filter(t -> false == file2Str.contains(t)).collect(Collectors.toList()); return filtedStr;}
上述是通過(guò)流的方式實(shí)現(xiàn)的,下面還有一種方法,通過(guò)BufferedReader,在讀取文件的時(shí)候也略有差異:
// 找出file1中有但是file2中沒(méi)有的數(shù)據(jù)public static List<String> filterData() throws IOException {// TODO Auto-generated method stubString file1Path = ".\\src\\main\\resources\\File1";String file2Path = ".\\src\\main\\resources\\File2";BufferedReader file1br = new BufferedReader(new FileReader(file1Path));BufferedReader file2br = new BufferedReader(new FileReader(file2Path));String line = "";List<String> file2String= new ArrayList<>();while ((line = file2br.readLine()) != null) {file2String.add(line);}while ((line = file1br.readLine()) != null) {if (false == file2String.contains(line)) {System.out.println(line);} else {file2String.remove(line);}} return file2String;}
?
文件格式樣例? File1:
String1
String2
String3
文件格式樣例? File2:
String1
String2
String4
轉(zhuǎn)載于:https://www.cnblogs.com/ilazysoft/p/6267103.html
總結(jié)
以上是生活随笔為你收集整理的找出文件1中有而文件2中没有的数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 红楼梦作者是谁啊?
- 下一篇: 图像处理复习整理(5.图像去模糊)