我想确保两个java.io.File没有指向同一个文件, 我尝试了各种方法,并最终找到了一种方法,但我想确保它周围没有漏洞。两个文件是否指向同一个文件?
这很重要,因为我正在编写一个程序来删除重复的文件,而且我不想因为两个java.io.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();
}
一边,我的try-catch代码有问题吗?当我跑步时它会给我一个警告。
它给出了哪个警告? –
问题是,捕获'例外'是危险的,并模糊其他问题 – ControlAltDel
检查此[链接](http://stackoverflow.com/questions/8214576/java7-path-file-get-a-unique-id-like对的一文件-an-i节点)。 – MCHAppy