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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31426187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:06:57  来源:igfitidea点击:

Want to find content difference between two text files with java

java

提问by aswini

I have two text files,

我有两个文本文件,

  • a.txt
  • b.txt
  • 一个.txt
  • b.txt

Each text files contains some file paths. b.txtcontains some more file paths than a.txt. I would like to determine which paths are added and which are removed from a.txtso that it corresponds to paths in b.txt.

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

For example,

例如,

abc.txt contains

abc.txt 包含

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

and xyz.txt contains

和 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

Now how to find that g.prop and h.prop are added and b.prop is removed?

现在如何发现添加了 g.prop 和 h.prop 并删除了 b.prop?

Could anyone explain how it is done? I could only find how to check for identical contents.

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

采纳答案by Kulbhushan Singh

The below code will serve your purpose irrespective of the content of the file.

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

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
            }
        }
    }

回答by Arunesh Singh

The memory will be a problem as you need to load both files into the program. I am using HashSetto ignore duplicates.Try this:

内存将是一个问题,因为您需要将两个文件加载到程序中。我正在使用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 <String> al = new HashSet<String>();
        HashSet <String> al1 = new HashSet<String>();
        HashSet <String> diff1 = new HashSet<String>();
        HashSet <String> diff2 = new HashSet<String>();
        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);
        }


    }

}

Output:

输出:

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

回答by Rajesh

Compare files [Scanner and ArrayList]:

比较文件 [Scanner 和 ArrayList]:

protected static void compareFiles(String firstFile, String secondFile)
        throws Exception {

    Scanner x = new Scanner(new File(firstFile));
    List<String> list1 = getScannerList(x);

    x = new Scanner(new File(secondFile));
    List<String> list2 = getScannerList(x);

    x.close();

    System.out.println("File Extras");
    printLnList(listExtras(list1, new ArrayList<String>(list2)));

    System.out.println("File Removals");
    printLnList(listExtras(list2, list1));  
}

protected static List<String> listExtras(List<String> list1,
        List<String> list2) throws Exception {      
    list2.removeAll(list1);
    return list2;
}

protected static List<String> getScannerList(Scanner sc) throws Exception {

    List<String> scannerList = new ArrayList<String>();

    while (sc.hasNext())
        scannerList.add(sc.nextLine());

    return scannerList;
}

protected static void printLnList(List<String> list) {
    for (String string : list)
        System.out.println(string);
}

Program output:

程序输出:

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

回答by Waqas Ikram

You can simple do follow

你可以简单地跟随

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<String> firstFileContent = Files.readAllLines(firstFile,
            Charset.defaultCharset());
        final List<String> secondFileContent = Files.readAllLines(secondFile,
            Charset.defaultCharset());

        System.out.println(diffFiles(firstFileContent, secondFileContent));
        System.out.println(diffFiles(secondFileContent, firstFileContent));
    }

    private static List<String> diffFiles(final List<String> firstFileContent,
        final List<String> secondFileContent) {
        final List<String> diff = new ArrayList<String>();
        for (final String line : firstFileContent) {
            if (!secondFileContent.contains(line)) {
                diff.add(line);
            }
        }
        return diff;
    }
}