1

There are two text files. I hope to know what is the most simplest JAVA way to check if their CONTENTS are equivalent each other.

On shell, I usually use diff command:

$ diff 1.txt 1.same.txt # nothing is shown if 1.txt and 1.same.txt have the same contents 
$ diff 1.txt 2.txt
4a5,6
> 20, -, 22.0
> 10, 10.0, -
Light Yagmi
  • 5,085
  • 12
  • 43
  • 64
  • 2
    What *exactly* do you mean by "equivalent"? Do they have to be byte-for-byte identical? Could they be the same Unicode code points but represented in different encodings? Could they differ in byte-order-mark? Could they differ in normalization form? "Equivalent" is a complicated term when it comes to text. – Jon Skeet Mar 30 '15 at 06:43
  • 1
    you could do something like [this](http://stackoverflow.com/questions/15441315/java-and-hash-algorithm-to-compare-files) – SomeJavaGuy Mar 30 '15 at 06:45

1 Answers1

1

Have a look at google-diff-match-patch, which implements Myer's diff.

Robust algorithms to perform the operations required for synchronizing plain text.

Diff: Compare two blocks of plain text and efficiently return a list of differences.

Match: Given a search string, find its best fuzzy match in a block of plain text. Weighted for both accuracy and location.

Patch: Apply a list of patches onto plain text. Use best-effort to apply patch even when the underlying text doesn't match.

AtomHeartFather
  • 954
  • 17
  • 35