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 invoking 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 is lexicographically less than, equal to, or greater than
the pathname of the argument File. The compareTo method has two overloaded forms: one takes a File
argument and the other takes an Object argument and so implements the Comparable interface.
Several boolean tests return information about the underlying file:
- exists returns TRue if the file exists in the file system.
- canRead returns true if a file exists and can be read.
- canWrite returns true if the file exists and can be written.
- isFile returns true if the file is not a directory or other special type of file.
- isDirectory returns TRue if the file is a directory.
- isAbsolute returns true if the path is an absolute pathname.
- isHidden returns TRue if the path is one normally hidden from users on the underlying system.
All the methods that inspect or modify the actual file system are security checked and can throw
SecurityException if you don't have permission to perform the operation. Methods that ask for the
filename itself are not security checked.
File objects have many other methods for manipulating files and directories. There are methods to inspect
and manipulate the current file:
public longlastModified()
Returns a long value representing the time the file was last modified or zero
if the file does not exist.
public longlength()
Returns the file length in bytes, or zero if the file does not exist.
public booleanrenameTo(File newName)
Renames the file, returning true if the rename succeeded.
public booleandelete()
Deletes the file or directory named in this File object, returning true if the