• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java APIs for Text file comparison

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nisha Soorya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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--
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David, I'd be very interested in that.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Up
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Tim gae
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right but the problem is that I have no documentation for use.
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://code.google.com/p/google-diff-match-patch/
 
reply
    Bookmark Topic Watch Topic
  • New Topic