Are two File pointing to the same file?
Asked Answered
L

1

5

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.

Lamelliform answered 23/4, 2015 at 13:16 Comment(3)
Which warning does it give?Valarievalda
The problem is that catching Exception is dangerous and ma obscure other problemsScales
Check this link.Frowst
F
8

Yes, this should work. From the documentation:

A canonical pathname is both absolute and unique. The precise definition of canonical form is system-dependent. This method first converts this pathname to absolute form if necessary, as if by invoking the getAbsolutePath() method, and then maps it to its unique form in a system-dependent way. This typically involves removing redundant names such as "." and ".." from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).

So this method should deal with:

  • redundant path parts such as /./
  • symlinks
  • relative paths

But I would strongly suggest you use Files.isSameFile:

If both Path objects are equal then this method returns true without checking if the file exists. If the two Path objects are associated with different providers then this method returns false. Otherwise, this method checks if both Path objects locate the same file, and depending on the implementation, may require to open or access both files.

Mainly because the java.io.File API has a large number of subtle bugs and API issues that cannot be worked around. But also because it has methods to do most common tasks built in.

Firedog answered 23/4, 2015 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.