My Java application needs the ability to compare two different files in the filesystem and decide if their binary contents are the same or not.
Here is my current code:
package utils;
import java.io.*;
class compare {
public static void main(String args[]) throws IOException {
FileInputStream file1 = new InputStream(args[0]);
FileInputStream file2 = new InputStream(args[1]);
try {
if(args.length != 2)
throw (new RuntimeException("Usage : java compare <filetoread> <filetoread>"));
while (true) {
int a = file1.read();
int b = file2.read();
if (a==-1) {
System.out.println("Both the files have same content");
}
else{
System.out.println("Contents are different");
}
}
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
Any tips or suggestions regarding how to make the comparison function correctly would be appreciated.