简体   繁体   中英

How to Check two csv/xml files are same or not in java

My Requirement

To check whether two csv/xml are same or not and I don't need differences.

No need to scan whole content and capture differences between two files. Just API has to return, once it finds first difference.

Please suggest me the best way for above.

Just compare the md5 to check if both have the same content

public class DemoApplication {

    public static void main(String[] args) throws IOException {

            InputStream file1 = Files.newInputStream(Paths.get("/Documents/hello.txt"));
            InputStream file2 = Files.newInputStream(Paths.get("/Documents/hello2.txt"));    

            String md5_file1 = org.apache.commons.codec.digest.DigestUtils.md5Hex(file1);
            String md5_file2 = org.apache.commons.codec.digest.DigestUtils.md5Hex(file2);

            if(md5_file1.equals(md5_file2)){
                System.out.println("Same file");
            }else{
                System.out.println("It's not the same file");
            }
    }
}

Are you looking for equality or literally if they are one and the same? There's a big difference!

In most cases, being the same can be determined by comparing creation time, modification time, name and exact file size.

In case of equality, just strip the times from the comparison.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM