Java 如何比较两个文本文件的内容并返回“相同内容”或“不同内容”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1510520/
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-12 13:39:29  来源:igfitidea点击:

How to compare the contents of two text files and return "Same Content" or "Different Content"?

java

提问by

My Java application needs the ability to compare two different files in the filesystem and decide if their binary contents are the same or not.

我的 Java 应用程序需要能够比较文件系统中的两个不同文件并确定它们的二进制内容是否相同。

Here is my current code:

这是我当前的代码:

package utils;
import java.io.*;

class compare { 
    public static void main(String args[]) throws IOException {
        FileInputStream file1 = new InputStream(args[0]);
        FileInputStream file2 = new InputStream(args[1]);

        try {
            if(args.length != 2)
                throw (new RuntimeException("Usage : java compare <filetoread> <filetoread>"));
            while (true) {
                int a = file1.read();
                int b = file2.read();
                if (a==-1) { 
                    System.out.println("Both the files have same content"); 
                }
                else{
                    System.out.println("Contents are different");
                }
            }
        }
        catch (Exception e) {
            System.out.println("Error: " + e);
        }
    }
}

Any tips or suggestions regarding how to make the comparison function correctly would be appreciated.

任何有关如何正确进行比较功能的提示或建议将不胜感激。

采纳答案by Brian Agnew

The simplest way is to read the contents into two strings e.g.

最简单的方法是将内容读入两个字符串,例如

  FileInputStream fin =  new FileInputStream(args[i]);
  BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
  StringBuilder sb = new StringBuilder();
  while ((thisLine = myInput.readLine()) != null) {  
             sb.append(thisLine);
  }

, and perform a .equals()on these. Do you require more complex differencing capabilities ?

,并对.equals()这些执行 a 。您是否需要更复杂的差分功能?

回答by Eric Petroelje

Read the contents of the files and use the MessageDigest class to create an MD5 hash of the contents of each file. Then compare the two hashes. This has the advantage of working for binary files as well.

读取文件的内容并使用 MessageDigest 类创建每个文件内容的 MD5 哈希。然后比较两个哈希值。这也具有适用于二进制文件的优点。

回答by varaprasad belide

import java.io.*;

public class Testing {
public static void main(String[] args) throws java.io.IOException {

    BufferedReader bfr2 = new BufferedReader(new InputStreamReader(
            System.in));
    String s1 = "";
    String s2 = "", s3 = "", s4 = "";
    String y = "", z = "";

    File file1 = new File("args[0]");
    File file2 = new File("args[1]");

    BufferedReader bfr = new BufferedReader(new FileReader(file1));
    BufferedReader bfr1 = new BufferedReader(new FileReader(file2));

    while ((z = bfr1.readLine()) != null)
        s3 += z;

    while ((y = bfr.readLine()) != null)
        s1 += y;

    System.out.println();

    System.out.println(s3);

    if (s3.equals(s1)) {
        System.out.println("Content of both files are same");
    } else {

        System.out.println("Content of both files are not same");
    }
}
}