I want to make sure that two java.io.File are not pointing to the same file, I have tried various methods, and finally found a way, but I want to make sure there is no loophole around it.
it's important because I am trying to write a program to delete duplicate files,and I don't want to end up deleting a unique file just because two java.io.File are pointing to the same file.
File f1 = new File("file.txt");
File f2 = new File("./file.txt");
//these methods can't tell it's the same file
System.out.println(f1.compareTo(f2)); // 56 which mean not equal
System.out.println(f1.equals(f2)); // false
System.out.println(f1 == f2); // false
System.out.println(f1.getAbsolutePath().compareTo(f2.getAbsolutePath())); // 56
// this method can tell it's the same file... hopefully.
try{
System.out.println(f1.getCanonicalPath().compareTo(f2.getCanonicalPath())); // 0
}catch (Exception e){
e.printStackTrace();
}
on the side, is there a problem with my try-catch code? it gives me a warning when I run.
Exception
is dangerous and ma obscure other problems – Scales