File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Java APIs for Text file comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java APIs for Text file comparison" Watch "Java APIs for Text file comparison" New topic
Author

Java APIs for Text file comparison

Nisha Soorya
Greenhorn

Joined: Dec 01, 2005
Posts: 11
Is there any java API available for comparint two text files?

What I want to do is to implement the compare functionality (as in TextPad) in Java Swing. Compare, and display the changes with a different color or highlight.

Any suggestion will be greatly appreciated. If APIs are not available please advice me how to do this.

Thanks,
Nisha
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39552
    
  27
A library I've been using is jlibdiff. It's a basic framework for comparing 2 (or even 3) files, but not well-documented. When I searched for something better with a Java API, I came up with nothing. You may have more luck running the "diff" tool directly (via Runtime.exec), and interpret the results in your application.


Ping & DNS - updated with new look and Ping home screen widget
Nisha Soorya
Greenhorn

Joined: Dec 01, 2005
Posts: 11
Thanks for your reply. Actually the solution I was looking for is exactly like the sample application provided with jlibdiff. But I have to incorporate the same functionality into another swing application.

But without any documentation, I don't know if can possible to use the same. The only document provided with that is in French. :-( .

My application needs to be executed in Windows. So I can't use diff.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39552
    
  27
My application needs to be executed in Windows. So I can't use diff.

If you can install stuff on the machine, have a look at unxutils, which provides many of the standard Unix tools on Windows.
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
You can use WinDiff from the NT tools disk from Microsoft after writing the two files to file. That's what I did anyway. I created a batch file that called my Java code that created the two files, then it went on to start windiff when my code ended.

Mine was a very quick and dirty app, but if you figure something out (find something on sourceforge, or make it yourself), I'd be interested in the source.

--Dale--
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here's a TextDiff I adapted from something on the web. Here's some of the doc:

TextDiff compares two text files or arrays of strings and generates a report of edit commands that would transform Old to New.

The DefaultReportWriter prints a human-friendly report to a PrintStream such as System.out. One could implement custom report writers to create machine-readable reports such as concrete editor commands.

Usage:

[ June 28, 2006: 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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39552
    
  27
Stan, that's interesting stuff. The test programs use the com.saxman.util.SSString class - do you know where that is available? Or does SSString.arrayToString exactly what its name implies?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Oops, forgot that gem was referenced in there. It's rather a monster with zillions of string functions and just about the first thing I wrote in Java so maybe I won't share the whole thing.

I use delimited strings with the delimiter first. That way you can use any delimiter you like, and even nest delimited strings with different delimiters. ArrayToString() has a list of candidate delimiters and finds one that is not in the data. Here's a bit of the unit tests:


Oh, yeah, there's a custom assertEquals() in there for two string arrays, too.

[ June 29, 2006: Message edited by: Stan James ]
[ June 29, 2006: Message edited by: Stan James ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39552
    
  27
Thanks Stan.

Now that I'm ready to get into it, I noticed that it also needs the com.saxman.util.TextFileIn class, which is not part of the download. Could I trouble you to make that available as well?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
TextFileIn is even older than SSString, truly one of the first things I did in Java, largely borrowed from something in TIJ I think. I'm too embarrased to read it but here it is ...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39552
    
  27
Now everything compiles fine - Thanks Stan!

Now I just need to figure out how it works, and how to replace jlibdiff with it - but that's the fun part
[ July 08, 2006: Message edited by: Ulf Dittmer ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Let me know if you fix any defects in that. If you're really going to use it for more than an educational example I'll look into my work baseline and see if I fixed any, too.
David W Brown
Greenhorn

Joined: Feb 13, 2008
Posts: 12
Hello, I know this is somewhat dated material but I was in the throes of a project and related solutions at my job when I have mavenized the TextDiff solution. If anyone is interested I can put this somewhere on the net. I have a blog that I have up-and-running but I have held off on putting any content on the blog. If there is any interest I can put the mavenized TextDiff files there such that folks can download TextDiff from the blog. Please advise, David.


Yet some, not wise, go to the other side of the globe, to barbarous and unhealthy regions, and devote ten or twenty years, in that they may live,-that is, keep comfortably warm,- and die in New England at last. Henry David Thoreau - Walden - 1845
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36520
    
  16
Thank you. I thought it was dated; Stan James died at the end of 2007.

Probably best to quote a link to your material here, please. Then anybody interested can look it up.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39552
    
  27
David, I'd be very interested in that.
Tim gae
Greenhorn

Joined: Sep 24, 2010
Posts: 3
Hello,

We are now in 2010 and I'would like to know if there are new libraries that can compare two texts and see their differences.

thank you,

PS: I have used jlibdiff, htmlDiff but they seem abandoned.
Tim gae
Greenhorn

Joined: Sep 24, 2010
Posts: 3
Up
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19545
    
  16

Tim gae wrote:PS: I have used jlibdiff, htmlDiff but they seem abandoned.

Is that a bad thing? I have one application on SourceForge which hasn't been updated since May 2007, simply because I consider it to be ready.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tim gae
Greenhorn

Joined: Sep 24, 2010
Posts: 3
Yes you are right but the problem is that I have no documentation for use.
J J Wright
Ranch Hand

Joined: Jul 02, 2008
Posts: 254
http://code.google.com/p/google-diff-match-patch/


SCJP, SCWCD, SCBCD, SCEA 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java APIs for Text file comparison
 
Similar Threads
Comparing 2 xml files
Yahoo Messenger API
Need to monitor MSNOutlook for incoming mail
IRC in java
API for Using MS Outlook from java