想用java找到两个文本文件之间的内容差异

我有两个文本文件,

  • A.TXT
  • b.txt

每个文本文件都包含一些文件路径。 b.txt包含比a.txt更多的文件路径。 我想确定添加哪些路径以及从a.txt中删除哪些路径,以便它对应于b.txt路径。

例如,

abc.txt包含

 E:\Users\Documents\hello\a.properties E:\Users\Documents\hello\b.properties E:\Users\Documents\hello\c.properties 

和xyz.txt包含

 E:\Users\Documents\hello\a.properties E:\Users\Documents\hello\c.properties E:\Users\Documents\hello\g.properties E:\Users\Documents\hello\h.properties 

现在如何找到g.prop和h.prop被添加并删除b.prop?

谁能解释它是如何完成的? 我只能找到如何检查相同的内容。

无论文件的内容如何,​​以下代码都将满足您的目的。

 import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class Test { public Test(){ System.out.println("Test.Test()"); } public static void main(String[] args) throws Exception { BufferedReader br1 = null; BufferedReader br2 = null; String sCurrentLine; List list1 = new ArrayList(); List list2 = new ArrayList(); br1 = new BufferedReader(new FileReader("test.txt")); br2 = new BufferedReader(new FileReader("test2.txt")); while ((sCurrentLine = br1.readLine()) != null) { list1.add(sCurrentLine); } while ((sCurrentLine = br2.readLine()) != null) { list2.add(sCurrentLine); } List tmpList = new ArrayList(list1); tmpList.removeAll(list2); System.out.println("content from test.txt which is not there in test2.txt"); for(int i=0;i 

内存将是一个问题,因为您需要将两个文件加载到程序中。 我正在使用HashSet来忽略重复项。 HashSet这个:

 import java.io.BufferedReader; import java.io.FileReader; import java.util.HashSet; public class FileReader1 { public static void main(String args[]) { String filename = "abc.txt"; String filename2 = "xyz.txt"; HashSet  al = new HashSet(); HashSet  al1 = new HashSet(); HashSet  diff1 = new HashSet(); HashSet  diff2 = new HashSet(); String str = null; String str2 = null; try { BufferedReader in = new BufferedReader(new FileReader(filename)); while ((str = in.readLine()) != null) { al.add(str); } in.close(); } catch (Exception e) { e.printStackTrace(); } try { BufferedReader in = new BufferedReader(new FileReader(filename2)); while ((str2 = in.readLine()) != null) { al1.add(str2); } in.close(); } catch (Exception e) { e.printStackTrace(); } for (String str3 : al) { if (!al1.contains(str3)) { diff1.add(str3); } } for (String str5 : al1) { if (!al.contains(str5)) { diff2.add(str5); } } for (String str4 : diff1) { System.out.println("Removed Path: "+str4); } for (String str4 : diff2) { System.out.println("Added Path: "+str4); } } } 

输出:

 Removed Path: E:\Users\Documents\hello\b.properties Added Path: E:\Users\Documents\hello\h.properties Added Path: E:\Users\Documents\hello\g.properties 

你可以简单地跟随

 import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; public class Test { public static void main(final String[] args) throws IOException { final Path firstFile = Paths.get("/home/src/main/resources/a.txt"); final Path secondFile = Paths.get("/home/src/main/resources/b.txt"); final List firstFileContent = Files.readAllLines(firstFile, Charset.defaultCharset()); final List secondFileContent = Files.readAllLines(secondFile, Charset.defaultCharset()); System.out.println(diffFiles(firstFileContent, secondFileContent)); System.out.println(diffFiles(secondFileContent, firstFileContent)); } private static List diffFiles(final List firstFileContent, final List secondFileContent) { final List diff = new ArrayList(); for (final String line : firstFileContent) { if (!secondFileContent.contains(line)) { diff.add(line); } } return diff; } } 

比较文件[Scanner和ArrayList]:

 protected static void compareFiles(String firstFile, String secondFile) throws Exception { Scanner x = new Scanner(new File(firstFile)); List list1 = getScannerList(x); x = new Scanner(new File(secondFile)); List list2 = getScannerList(x); x.close(); System.out.println("File Extras"); printLnList(listExtras(list1, new ArrayList(list2))); System.out.println("File Removals"); printLnList(listExtras(list2, list1)); } protected static List listExtras(List list1, List list2) throws Exception { list2.removeAll(list1); return list2; } protected static List getScannerList(Scanner sc) throws Exception { List scannerList = new ArrayList(); while (sc.hasNext()) scannerList.add(sc.nextLine()); return scannerList; } protected static void printLnList(List list) { for (String string : list) System.out.println(string); } 

节目输出:

 File Extras E:\Users\Documents\hello\g.properties E:\Users\Documents\hello\h.properties File Removals E:\Users\Documents\hello\b.properties