比较Java中的两个文档 [英] Compare two documents in Java

查看:141
本文介绍了比较Java中的两个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个要在java中进行比较的word文档. 我尝试使用

I have two word documents which i am trying to compare in java . I tried using

md5哈希码

HashCode newFile = Files.asByteSource(newFileInput).hash(Hashing.md5());
HashCode oldFile = Files.asByteSource(oldFileInput).hash(Hashing.md5());

并同时使用

布尔值isEqual = FileUtils.contentEquals(oldFile,newFile);

即使内容相同,还是使用在线工具对内容进行了比较,无可比拟, 上面两种方法中的哈希码仍然是MISMATCH.

Even though the contents are same ,compared the content using online tools and beyond compare, still the hashcode in both above method comes as MISMATCH.

有什么解决方案吗?或使用Java中的任何API比较任何文件类型的方法. 我需要在两个单词文件之间进行深层比较,如空格,字体,内容.等.

any solutions? or way to compare any file type using any API in Java. i need to do deep compare between two word files as in for spaces,fonts , content. etc..

预期结果:两个文件都应匹配

Expected Result : Both file should match

推荐答案

即使两个文档看起来相同,或者即使两个文档都包含相同格式的内容,也可能对最后修改日期(如上次修改日期)稍作更改,否则将导致比较失败. JSON文档更易于比较,但Word文档是二进制的.最小的更改可以完全更改文档.

Even if both of your documents look the same or even if both contains the same formatted content, a slightly change like the last modified date will result in a failed comparison. JSON documents are more easier to compare but Word documents are binary. The smallest change can change the document completely.

因此,您必须采用困难的方式:找到一个库来自己读取Word文件的内容,并专门检查两个文件的内容.

So you have to do it the hard way: Find a library to read the content of the Word files by yourself and check the content of both files specifically.

这篇关于比较Java中的两个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆