Advertisement
Guest User

JDiff

a guest
Mar 6th, 2021
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. /**
  5.  * Diff program which can handle files of any size.
  6.  */
  7. public class JDiff
  8. {
  9.     private BufferedInputStream stream1 = null;
  10.     private BufferedInputStream stream2 = null;
  11.     /**
  12.      * Constructor for objects of class JDiff
  13.      */
  14.     public JDiff(String[] args)
  15.     {
  16.         try
  17.         {
  18.             File file1 = new File(System.getProperty("user.dir") + "/" + args[0]);
  19.             File file2 = new File(System.getProperty("user.dir") + "/" + args[1]);
  20.             if (file1 == null || file2 == null)
  21.             {
  22.                 System.out.println("One or both of these files are not valid.");
  23.                 System.out.println(file1.toString());
  24.                 System.out.println(file2.toString());
  25.                 System.out.println("Please select two valid files.");
  26.             }
  27.             if (file1.getAbsolutePath().equals(file2.getAbsolutePath()))
  28.             {//same path, same file
  29.                 System.out.println("These are the exact same file.");
  30.                 System.out.println(file1.toString());
  31.                 System.out.println("Please choose two different files.");
  32.                 return;
  33.             }
  34.             System.out.println(file1);
  35.             System.out.println(file2);
  36.             stream1 = new BufferedInputStream(new FileInputStream(file1));
  37.             stream2 = new BufferedInputStream(new FileInputStream(file2));
  38.             if (file1.length() == file2.length())
  39.             {//files are of the same length
  40.                 int byte1 = (stream1.read() & 0xFF);
  41.                 int byte2 = (stream2.read() & 0xFF);
  42.                 long fileCntr = 0;
  43.                 while ((byte1 != -1) && (byte2 != -1))
  44.                 {
  45.                     if (byte1 != byte2)
  46.                     {
  47.                         System.out.print("Address: 0x" + Long.toHexString(fileCntr).toUpperCase());
  48.                         if (byte1 < 16)
  49.                         {
  50.                            System.out.print("\t0x0" + Integer.toHexString(byte1));
  51.                         }
  52.                         else //byte >= 16
  53.                         {
  54.                            System.out.print("\t0x" + Integer.toHexString(byte1));
  55.                         }
  56.                         if (byte2 < 16)
  57.                         {
  58.                            System.out.print("\t0x0" + Integer.toHexString(byte2) + "\n");
  59.                         }
  60.                         else //byte >= 16
  61.                         {
  62.                            System.out.print("\t0x" + Integer.toHexString(byte2) + "\n");
  63.                         }
  64.                     }
  65.                     byte1 = stream1.read();
  66.                     byte2 = stream2.read();
  67.                     fileCntr ++;
  68.                 }
  69.             }
  70.             else
  71.             {//files aren't the same length
  72.                 ;
  73.             }
  74.             System.out.println();
  75.         }
  76.         catch (Exception e)
  77.         {
  78.             e.printStackTrace();
  79.         }
  80.         finally
  81.         {
  82.             if (stream1 != null)
  83.             {
  84.                 try
  85.                 {
  86.                     stream1.close();
  87.                 }
  88.                 catch (Exception e) {}
  89.             }
  90.             if (stream2 != null)
  91.             {
  92.                 try
  93.                 {
  94.                     stream2.close();
  95.                 }
  96.                 catch (Exception e) {}
  97.             }
  98.         }
  99.     }
  100.  
  101.     public static void main(String[] args)
  102.     {
  103.         JDiff jdiff = new JDiff(args);
  104.     }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement