The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.io  [35 examples] > Filenames and Pathnames  [6 examples]

e16. Determining If Two Filename Paths Refer to the Same File

A filename path may include redundant names such as `.' or `..' or symbolic links (on UNIX platforms). File.getCanonicalFile() converts a filename path to a unique canonical form suitable for comparisons.
    File file1 = new File("./filename");
    File file2 = new File("filename");
    
    // Filename paths are not equal
    boolean b = file1.equals(file2);      // false
    
    // Normalize the paths
    try {
        file1 = file1.getCanonicalFile(); // c:\almanac1.4\filename
        file2 = file2.getCanonicalFile(); // c:\almanac1.4\filename
    } catch (IOException e) {
    }
    
    // Filename paths are now equal
    b = file1.equals(file2);              // true

 Related Examples
e13. Constructing a Filename Path
e14. Converting Between a Filename Path and a URL
e15. Getting an Absolute Filename Path from a Relative Filename Path
e17. Getting the Parents of a Filename Path
e18. Determining If a Filename Path Is a File or a Directory

See also: Directories    Encodings    Files    Parsing    Reading and Writing    Serialization   


© 2002 Addison-Wesley.