Having read your other post, I'm not sure if you are trying to find the difference between two java source files, or write a java program to find the difference between to arbitrary files? [ February 25, 2005: Message edited by: Horatio Westock ]
Here's an algorithm I found. The code was a literal translation of C to Java, very nasty. I did my own implementation that is slightly less nasty. I'd be happy to share the code if you're not doing a school assignment, but you might have more fun exploring on your own.
For each each unique line of text create a symbol. Two lines that are equals() share a symbol. The symbol state is: OldOnly, NewOnly, UniqueMatch (both files exactly once), or Other.
For each line, create a LineInfo object. Set state = symbol state and establish bidirectional links between UniqueMatch lines in the two files.
For each UniqueMatch in old create a "match block". Stretch match blocks forward and backward to include matching lines with any state, including other match blocks.
Build a Report of edit commands that can be used to tranform Old into New. Matching blocks generate match or move commands. Non-matching blocks generate insert, append, delete or change commands. (this is the triky bit, but only 14 lines of code.)
Iterate the commands to generate a report.
Edit: Just remembered I had a web page on this: Text Diff. The zip of my source is missing in action - sorry about that. The link to the code I read for the algorithm is there but I would not advise copying any of it. [ February 25, 2005: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Jan 29, 2003
If anyone cares, I fixed the link to the source zip: TextDiff.zip.