home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 25 / IOPROG_25.ISO / SOFT / JavaS / javastar-eval.exe / data1.cab / Program_Files / contrib / utils / Compare.java < prev    next >
Encoding:
Java Source  |  1999-02-11  |  1.7 KB  |  55 lines

  1. /*
  2.  *  @version             @(#)Compare.java    1.2 98/05/15
  3.  *
  4.  * Copyright (c) 1997-1998 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. import java.io.*;
  21.  
  22. public class Compare  {
  23.  
  24.   /*
  25.    * compare file name1 and name2 one bye at a time
  26.    */
  27.   public static boolean compare(String name1, String name2) throws IOException {
  28.     FileInputStream file1 = new FileInputStream(new File(name1));
  29.     FileInputStream file2 = new FileInputStream(new File(name2));
  30.     return compare(file1, file2);
  31.   }
  32.  
  33.   /*
  34.    * compare file1 and file2 one bye at a time
  35.    */
  36.   public static boolean compare(FileInputStream file1,
  37.                 FileInputStream file2) throws IOException {
  38.     int b1 = file1.read();
  39.     int b2 = file2.read();
  40.     while (b1 >= 0 && b2 >= 0) {
  41.       if (b1 != b2)
  42.     return false;
  43.  
  44.       b1 = file1.read();
  45.       b2 = file2.read();
  46.     }
  47.  
  48.     return (b1 < 0 && b2 < 0);
  49.   }
  50.  
  51.   public static void main(String[] args) throws IOException {
  52.     System.out.println("The files are " + (Compare.compare(args[0], args[1]) ? "equal" : "not equal"));
  53.   }
  54. }
  55.