tags:

views:

184

answers:

3

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.

+5  A: 

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

Jim Garrison
A: 

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

Am
This isn't any good for two reasons: One, he's expecting there to be one different field (the GUID he mentioned); two, md5 can technically collide, so equal checksums in no way necessarily implies equal input to the checksum function.
Platinum Azure
which is why i recommend md5, a known fast algorithm. the probability for collision, with even a slight similarity - the xml header - is infinitely smaller then the time i takes to fully compare two text files.
Am
It's worth noting that MD5 is somewhat broken. IIRC it's possible to pad a document to come up with what hash you want. Also, if operations must be performed on all characters, and the OP has the option of reading in and comparing all characters (after a file size check - if they're different sizes, it might not be worth going further), then a simple byte-by-byte comparison would seem to be less operationally intensive than a f(each-byte-from-file1) == f(each-byte-from-file2)
atk
The hashing approach is useful if you're comparing many files trying to find duplicates, but if you're just trying to check if two files are identical it's not useful at all. You need to read the entire file to hash it, so you may as well read the two files concurrently and compare them byte by byte. (And that's assuming the question was about checking for exact matches, which it seems this question isn't given the "guid" blurb.)
Laurence Gonsalves
Who upvoted his reply comment? As Laurence Gonsalves correctly points out, he's not even looking for purely identical files in the first place.
Platinum Azure
+1  A: 

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)

Platinum Azure