Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Java Program for File Differences

 
Ranch Hand
Posts: 224
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've spent a lot of my career as a software engineer working with Unix or Linux operating systems. Whenever I've had two files with just minor differences between them, I could type in at the command prompt:

diff file1.Txt file2.Txt

and I'd get a listing of all the differences between the two files, looking something like:

< the quick brown fox jumps over the lazy dog
--
> the quick brown fox jumped over the lazy dogs

However, now I'm stuck with Windows 7, that, as far as I know, doesn't have an analogous diff command (correct me if I'm wrong). In a situation like this, it would be very nice if there existed a Java program itself that would do more or less what the Unix diff did for me before. Part of me is tempted to try to write such a Java program, but before I did that I thought I should probably come to this forum and ask everyone if such a Java program already exists. No sense reinventing the wheel. Does anybody know of any such Java program?
 
Marshal
Posts: 26314
80
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know about a Java version of diff, but when I read the Wikipedia article about the diff utility it mentioned WinMerge as a similar Windows-based tool.
 
Kevin Simonson
Ranch Hand
Posts: 224
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took a look at Nicholas Butler's description of Myer's algorithm for use in finding the differences between two character arrays, in the hopes that I could use it to find the differences between two string arrays, and thus find the differences between two files. That description is at "http://www.codeproject.com/Articles/42279/Investigating-Myers-diff-algorithm-Part-of". I was following what he was saying until he got to the subsection labeled "d contours" under section "Definitions". Does anyone else understand what a "d contour" is in light of the other definitions?

Not far down from there Butler lists code that looks like C# in what he calls "the core implementation":

and another section he calls the "solution":

but it's not clear how these two pieces fit together, even if they could be translated into Java. Anybody have any ideas?
 
Saloon Keeper
Posts: 12631
274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, beginning to try to understand that code is futile. Now you see the importance of giving your variables clear names.
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are many choices for getting Windows ports of diff. All work exactly like *nix diff.

1. GnuWin32 has a diff port. Can be added to PATH.

2. Install Git for windows. It installs a "Git bash" shortcut. Launch it and run diff from that console. Not sure if diff can be directly added to PATH.

3. MinGW's msys component has a diff. Can be added to PATH.

4. Cygwin. Not sure if diff can be directly added to PATH.

 
Kevin Simonson
Ranch Hand
Posts: 224
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finally found a Java program that does exactly what I needed the "diff" utility to do, at URL "introcs.cs.princeton.edu/java/96optimization/Diff.java.html". I modified it slightly to do what I wanted it to do, and to fit my style, and came up with:

Feel free to try it out. I should warn you that if the files you are comparing both have much more than ten thousand lines, this program runs out of Java heap space.
 
reply
    Bookmark Topic Watch Topic
  • New Topic