joe
joe

Reputation: 17488

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?

I am planning on having a guid as the only difference in the files.

Upvotes: 0

Views: 2818

Answers (3)

Platinum Azure
Platinum Azure

Reputation: 46233

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.

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.

If you want line-by-line and you're using Java 1.5 or newer, look into java.util.Scanner; otherwise look into java.io.FileReader for character block reading.

(Note: If you don't have Java 1.5 or newer, you're stuck with FileReader)

Upvotes: 1

Amirshk
Amirshk

Reputation: 8258

You could run md5 on both files and compare the values.

Upvotes: 0

Jim Garrison
Jim Garrison

Reputation: 86774

Here's a link to a list of Open Source XML diff tools written in Java

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

Upvotes: 5

Related Questions