简体   繁体   English

如何在Java中检测两个文本文件是否相同

[英]How to detect if two text files are identical in java

I would like to detect if file1.xml is different than file1.xmlCheck is there a way to do this in java? 我想检测file1.xml是否不同于file1.xmlCheck,是否可以在Java中执行此操作?

I am planning on having a guid as the only difference in the files. 我打算将Guid作为文件中的唯一区别。

Here's a link to a list of Open Source XML diff tools written in Java 这是指向用Java编写的开放源XML差异工具列表的链接。

http://www.manageability.org/blog/stuff/open-source-xml-diff-in-java http://www.manageability.org/blog/stuff/open-source-xml-diff-in-java

You might as well try the obvious and read the files in. You can read them character by character, block by block (say 1024 or 4096 characters at a time), or line by line and compare the strings you get. 您也可以尝试使用显而易见的文件并读取其中的文件。您可以逐个字符地,逐个块地(例如一次为1024或4096个字符)或逐行地读取它们并比较所得到的字符串。

If you know the exact location of your GUID and it's near the beginning or whatnot, you can build that into your comparison as well. 如果您知道GUID的确切位置,并且该位置就在开始位置附近,也可以在比较中使用。

If you want line-by-line and you're using Java 1.5 or newer, look into java.util.Scanner ; 如果要逐行并且使用Java 1.5或更高版本,请查看java.util.Scanner otherwise look into java.io.FileReader for character block reading. 否则,请查看java.io.FileReader以读取字符块。

(Note: If you don't have Java 1.5 or newer, you're stuck with FileReader) (注意:如果您没有Java 1.5或更高版本,则必须使用FileReader。)

您可以在两个文件上运行md5并比较这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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