Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- /**
- * Diff program which can handle files of any size.
- */
- public class JDiff
- {
- private BufferedInputStream stream1 = null;
- private BufferedInputStream stream2 = null;
- /**
- * Constructor for objects of class JDiff
- */
- public JDiff(String[] args)
- {
- try
- {
- File file1 = new File(System.getProperty("user.dir") + "/" + args[0]);
- File file2 = new File(System.getProperty("user.dir") + "/" + args[1]);
- if (file1 == null || file2 == null)
- {
- System.out.println("One or both of these files are not valid.");
- System.out.println(file1.toString());
- System.out.println(file2.toString());
- System.out.println("Please select two valid files.");
- }
- if (file1.getAbsolutePath().equals(file2.getAbsolutePath()))
- {//same path, same file
- System.out.println("These are the exact same file.");
- System.out.println(file1.toString());
- System.out.println("Please choose two different files.");
- return;
- }
- System.out.println(file1);
- System.out.println(file2);
- stream1 = new BufferedInputStream(new FileInputStream(file1));
- stream2 = new BufferedInputStream(new FileInputStream(file2));
- if (file1.length() == file2.length())
- {//files are of the same length
- int byte1 = (stream1.read() & 0xFF);
- int byte2 = (stream2.read() & 0xFF);
- long fileCntr = 0;
- while ((byte1 != -1) && (byte2 != -1))
- {
- if (byte1 != byte2)
- {
- System.out.print("Address: 0x" + Long.toHexString(fileCntr).toUpperCase());
- if (byte1 < 16)
- {
- System.out.print("\t0x0" + Integer.toHexString(byte1));
- }
- else //byte >= 16
- {
- System.out.print("\t0x" + Integer.toHexString(byte1));
- }
- if (byte2 < 16)
- {
- System.out.print("\t0x0" + Integer.toHexString(byte2) + "\n");
- }
- else //byte >= 16
- {
- System.out.print("\t0x" + Integer.toHexString(byte2) + "\n");
- }
- }
- byte1 = stream1.read();
- byte2 = stream2.read();
- fileCntr ++;
- }
- }
- else
- {//files aren't the same length
- ;
- }
- System.out.println();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (stream1 != null)
- {
- try
- {
- stream1.close();
- }
- catch (Exception e) {}
- }
- if (stream2 != null)
- {
- try
- {
- stream2.close();
- }
- catch (Exception e) {}
- }
- }
- }
- public static void main(String[] args)
- {
- JDiff jdiff = new JDiff(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement