this code is used to compare the content of 2 text files,
but i need to validate first, that the names of those 2 file is not same.
can you help me to fix this code?
import java.io.*;
import java.util.*;
public class compare {
public static void main(String args[]) throws IOException {
try{
FileInputStream file1 = new FileInputStream(args[0]);
FileInputStream file2 = new FileInputStream(args[1]);
if(args.length != 2)
throw (new RuntimeException("Usage : java compare <filetoread> <filetoread>"));
if(args[0]==args[1]){
System.out.print("File name is identical");
}
else {
while (true) {
int a = file1.read();
int b = file2.read();
if (a!=b) {
System.out.print("File do not match");
break;
}
else{
System.out.print("Files match");
break;
}
}
}
}
catch(FileNotFoundException e){
System.out.print("File tidak ada");
}
catch(IOException e){
System.out.print("IO Error");
}
}
}
would help to compare the content of the String rather than their addresses.
Actually
is better (if you can get File objects ‘
new File(arg[0])‘, which can then be used as an argument for yourFileInputStreamobjects): you will compare two canonical normalized path, rather than two String entered by a user with potential lowercase/uppercase differences for instance. SeegetCanonicalPath()javadoc: