Java Reference
In-Depth Information
+ src.getAbsolutePath());
System.out.println("getCanonicalPath() = "
+ src.getCanonicalPath());
System.out.println("getParent() = " + src.getParent());
And here is the output:
getName() = FileInfo.java
getPath() = ../ok/FileInfo.java
getAbsolutePath() = /vob/java_prog/src/../ok/FileInfo.java
getCanonicalPath() = /vob/java_prog/ok/FileInfo.java
getParent() = ../ok
The canonical path is defined by each system. Usually, it is a form of
the absolute path with relative components (such as
".."
to refer to the
parent directory) renamed and with references to the current directory
removed. Unlike the other "get" methods,
getCanonicalPath
can throw
IOException
because resolving path components can require calls to the
underlying file system, which may fail.
The methods
getParentFile
,
getAbsoluteFile
, and
getCanonicalFile
are
analogous to
getParent
,
getAbsolutePath
, and
getCanonicalPath
, but they
return
File
objects instead of strings.
You can convert a
File
to a
java.net.URL
or
java.net.URI
object by invok-
ing
toURL
or
toURI
, respectively.
The overriding method
File.equals
deserves mention. Two
File
objects
are considered equal if they have the same path, not if they refer to
the same underlying file system object. You cannot use
File.equals
to
test whether two
File
objects denote the same file. For example, two
File
objects may refer to the same file but use different relative paths to
refer to it, in which case they do not compare equal. Relatedly, you can
compare two files using the
compareTo
method, which returns a number
less than, equal to, or greater than zero as the current file's pathname