aspose file tools*
The moose likes Java in General and the fly likes How to find Difference between two files in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to find Difference between two files in java" Watch "How to find Difference between two files in java" New topic
Author

How to find Difference between two files in java

nagaraj reddy
Ranch Hand

Joined: Feb 21, 2005
Posts: 43
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

Joined: Feb 23, 2005
Posts: 221
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

Joined: Jan 29, 2003
Posts: 8791
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If anyone cares, I fixed the link to the source zip: TextDiff.zip.
Shashikanth Channagiri
Greenhorn

Joined: Feb 21, 2013
Posts: 1
http://javacook.darwinsys.com/javasrc/textproc/Diff.java - Cached

Great Java Diff
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36590
    
  16
Thank you for the link, which I converted to a hyperlink. And welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find Difference between two files in java
 
Similar Threads
Time Stamp values
about dates
plz help me info regarding finding differences in two java java
how do I find date difference in months
Bean class not found