When your path starts with a root dir i.e. C:\
in windows or /
in Unix or in java resources path, it is considered to be an absolute path. Everything else is relative, so
new File("test.txt") is the same as new File("./test.txt")
new File("test/../test.txt") is the same as new File("./test/../test.txt")
The major difference between getAbsolutePath
and getCanonicalPath
is that the first one concatenates a parent and a child path, so it may contain dots: ..
or .
. getCanonicalPath
will always return the same path for a particular file.
Note: File.equals
uses an abstract form of a path (getAbsolutePath
) to compare files, so this means that two File
objects for the same might not be equal and File
s are unsafe to use in collections like Map
or Set
.