JavaDiff - version 1.1


1. General Information

Version: 1.1
Date: 24th April 2001

JavaDiff is a simple command-line utility for comparing two files, or for recursively comparing the contents of two directories. it also provides an API for embedding the functionality within another Java application.

The program will attempt to report a minimal set of line differences for ASCII files. For binary files, it will simply report whether or not they are different.

2. Command-line Usage

java -jar JavaDiff.jar file1 file2

All differences will be reported to the standard output stream.

2.1 Return codes

The program will return the following status codes on exit:

  Files are identical:           0
  Files differ:                  1
  File1 not found:               2
  File2 not found:               3
  Unknown status:                4
  Abnormal program termination: -1

3. Programmatic Usage

The utility also provides a simple API for controlling from within another Java program.

3.1 API

The API is as follows:

    package com.jj.javadiff;

    public class Diff
      public Diff();
      public Diff(String first_pathname, String second_pathname);

      public void Compare();
      public void SetFirstFile(String pathname);
      public void SetSecondFile(String pathname);
      public void SetWriter( writer);
      public int  GetComparisonStatus();

      final public static int COMPARISON_FILES_IDENTICAL;
      final public static int COMPARISON_FILES_DIFFER;
      final public static int COMPARISON_FIRST_FILE_NOT_FOUND;
      final public static int COMPARISON_SECOND_FILE_NOT_FOUND;
      final public static int COMPARISON_UNKNOWN;

3.2 Sample Code

    import com.jj.javadiff.Diff;


      // Construct the diff object
      Diff diff = new Diff();

      // Supply the two files or directories to compare

      // Set the output stream for reporting differences
      // Here we are asking for the output to go to standard out. pw = new;

      // Perform the comparison

      // Flush the output buffer

      // Get the return status
      switch (diff.GetComparisonStatus())
        case diff.COMPARISON_FILES_IDENTICAL:       // The files match exactly
        case diff.COMPARISON_FILES_DIFFER:          // The files differ
        case diff.COMPARISON_FIRST_FILE_NOT_FOUND:  // The first file was not found
        case diff.COMPARISON_SECOND_FILE_NOT_FOUND: // The second file was not found
        case diff.COMPARISON_UNKNOWN:               // The comparison status is unknown
    catch (Exception e)
      // The program terminated abnormally

4. Download

The following zip file contains the jar file for JavaDiff. This should be downloaded and extracted to your local drive. (7 KB)