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

asw*_*ini 1 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 
Run Code Online (Sandbox Code Playgroud)

和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
Run Code Online (Sandbox Code Playgroud)

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

任何人都可以解释它是如何完成的?我只能找到如何检查相同的内容.

Kul*_*ngh 8

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

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<String> list1 = new ArrayList<String>();
            List<String> list2 = new ArrayList<String>();
            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<String> tmpList = new ArrayList<String>(list1);
            tmpList.removeAll(list2);
            System.out.println("content from test.txt which is not there in test2.txt");
            for(int i=0;i<tmpList.size();i++){
                System.out.println(tmpList.get(i)); //content from test.txt which is not there in test2.txt
            }

            System.out.println("content from test2.txt which is not there in test.txt");

            tmpList = list2;
            tmpList.removeAll(list1);
            for(int i=0;i<tmpList.size();i++){
                System.out.println(tmpList.get(i)); //content from test2.txt which is not there in test.txt
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)