• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find Difference between two files in java

 
nagaraj reddy
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need information regarding How to find out difference between two files in java interms of Modified Lines ,newly Added lines nad deleted lines.
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be of interest: http://www.bmsi.com/java/#diff

Remember to read the licence stuff!

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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If anyone cares, I fixed the link to the source zip: TextDiff.zip.
 
Shashikanth Channagiri
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://javacook.darwinsys.com/javasrc/textproc/Diff.java - Cached

Great Java Diff
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the link, which I converted to a hyperlink. And welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic