• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Program for File Differences  RSS feed

 
Kevin Simonson
Ranch Hand
Posts: 223
2
  • Mark post as helpful
  • send pies
  • 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?
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 223
2
  • Mark post as helpful
  • send pies
  • 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?
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • 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.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • 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: 223
2
  • Mark post as helpful
  • send pies
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!