We can create a file with a relative pathname:
That file, if it exists, is in the directory above the current working directory. Now if we want to know its path from the root directory, we use getAbsolutePath(). This method makes no attempt to resolve the path so it will be the curent working directory, two dots, then the file name (i.e. c:\java\..\test.txt). This could cause confusion should we attempt to create another file in the
java directory with the same name:
Both of the files point to the same directory but the paths are different. This makes comparing files for equality (i.e. do two File instances represent the same physical file) impossible. getCanonicalPath() and getCanonicalFile() compute the path by resolving path operators (i.e. . and ..), symbolic links, drive letter case (on Windows) and so on such that a single file on a given file system has a single canonical path. We can use this canonical path (or instance, in the case of getCanonicalFile) to reliably
test File instances for equality.
[ February 15, 2005: Message edited by: Joe Ess ]