要使用java查找两个文本文件之间的内容差异吗

要使用java查找两个文本文件之间的内容差异吗,java,Java,我有两个文本文件 a、 文本 b、 文本 每个文本文件都包含一些文件路径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 E:\Users\Documents\hel

我有两个文本文件

  • a、 文本
  • b、 文本
每个文本文件都包含一些文件路径
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 
E:\Users\Documents\hello\a.properties
E:\Users\Documents\hello\c.properties
E:\Users\Documents\hello\g.properties
E:\Users\Documents\hello\h.properties
并且xyz.txt包含

E:\Users\Documents\hello\a.properties
E:\Users\Documents\hello\b.properties
E:\Users\Documents\hello\c.properties 
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<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
            }
        }
    }
导入java.io.BufferedReader;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
公开课考试{
公开考试(){
System.out.println(“Test.Test()”);
}
公共静态void main(字符串[]args)引发异常{
BufferedReader br1=null;
BufferedReader br2=null;
弦电流线;
List list1=新的ArrayList();
List list2=新的ArrayList();
br1=新的BufferedReader(新的文件读取器(“test.txt”);
br2=新的BufferedReader(新的文件读取器(“test2.txt”);
而((sCurrentLine=br1.readLine())!=null){
列表1.添加(当前行);
}
而((sCurrentLine=br2.readLine())!=null){
列表2.添加(当前行);
}
List tmpList=新的ArrayList(list1);
tmpList.removeAll(列表2);
System.out.println(“test2.txt中没有的test.txt中的内容”);

对于(int i=0;i,内存将是一个问题,因为您需要将两个文件加载到程序中。 我正在使用
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);
        }


    }

}

比较文件[扫描仪和阵列列表]:

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

你可以做一些简单的事情

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;
    }
}
import java.io.IOException;
导入java.nio.charset.charset;
导入java.nio.file.Files;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.util.ArrayList;
导入java.util.List;
公开课考试{
公共静态void main(最终字符串[]args)引发IOException{
final Path firstFile=Path.get(“/home/src/main/resources/a.txt”);
final Path secondFile=Path.get(“/home/src/main/resources/b.txt”);
最终列表firstFileContent=Files.readAllLines(firstFile,
defaultCharset());
最终列表secondFileContent=Files.readAllLines(secondFile,
defaultCharset());
System.out.println(diffFiles(firstFileContent,secondFileContent));
System.out.println(diffFiles(secondFileContent,firstFileContent));
}
私有静态列表diffFiles(最终列表firstFileContent,
最终列表(文件内容){
最终列表差异=新的ArrayList();
for(最后一行字符串:firstFileContent){
如果(!secondFileContent.contains(行)){
差异添加(行);
}
}
返回差;
}
}

您能举个例子吗?您读取第一个文件内容并存储在变量[列表]中然后逐行读取第二条路径,将两条路径进行比较,并确定两条路径的交叉点等。如Lance Java所述,将a的路径存储在a集中,将b的路径存储在b集中。然后,交叉点b是公共路径,b setdiff a是存在于b中但不存在于a中的路径集,而setdiff b则相反。显示t他对这两个文件的内容进行了示例。你可以直接复制粘贴代码,并在两个缓冲区读取器中给出文件的路径,这样就可以了。但基本上,我假设一行是一个字符串,并对其进行比较。看看这是否有助于你感谢你。但是,这里删除的路径出现在添加的路径之间。为什么它们不能分开。我不知道知道你所说的添加或删除路径是什么意思吗?你能说得更清楚吗?如果你说的是我所理解的,请检查更新。添加行号:
diff.add((firstFileContent.indexOf(line)+1)++“”+line);