User interfaces and operating systems use system-dependent pathname strings to name files and directories. This class presents an abstract, system-independent view of hierarchical pathnames. An abstract pathname has two components:
"/" for the UNIX root
directory, or "\\\\" for a Microsoft Windows UNC pathname, and
The conversion of a pathname string to or from an abstract pathname is
inherently system-dependent. When an abstract pathname is converted into a
pathname string, each name is separated from the next by a single copy of
the default separator character. The default name-separator
character is defined by the system property file.separator, and
is made available in the public static fields separator and separatorChar of this class.
When a pathname string is converted into an abstract pathname, the names
within it may be separated by the default name-separator character or by any
other name-separator character that is supported by the underlying system.
A pathname, whether abstract or in string form, may be either
absolute or relative. An absolute pathname is complete in
that no other information is required in order to locate the file that it
denotes. A relative pathname, in contrast, must be interpreted in terms of
information taken from some other pathname. By default the classes in the
java.io package always resolve relative pathnames against the
current user directory. This directory is named by the system property
user.dir, and is typically the directory in which the Java
virtual machine was invoked.
Many operating systems and file systems have support for symbolic links. A symbolic link is a special file that serves as a reference to another file. Unless otherwise specified, symbolic links are transparent to applications and operations on files that are symbolic links are automatically redirected to the target of the link. Methods that only operate on the abstract pathname do not access the file system and thus do not resolve symbolic links.
The parent of an abstract pathname may be obtained by invoking
the getParent method of this class and consists of the pathname's
prefix and each name in the pathname's name sequence except for the last.
Each directory's absolute pathname is an ancestor of any File
object with an absolute abstract pathname which begins with the directory's
absolute pathname. For example, the directory located by the abstract
pathname "/usr" is an ancestor of the directory located by the
pathname "/usr/local/bin".
The prefix concept is used to handle root directories on UNIX platforms, and drive specifiers, root directories and UNC pathnames on Microsoft Windows platforms, as follows:
"/". Relative pathnames have no prefix. The abstract pathname
denoting the root directory has the prefix "/" and an empty
name sequence.
":" and
possibly followed by "\\" if the pathname is absolute. The
prefix of a UNC pathname is "\\\\"; the hostname and the share
name are the first two names in the name sequence. A relative pathname that
does not specify a drive has no prefix.
Instances of this class may or may not locate an actual file-system object such as a file or a directory. If it does locate such an object then that object resides in a partition. A partition is an operating system-specific portion of storage for a file system. A single storage device (e.g. a physical disk-drive, flash memory, CD-ROM) may contain multiple partitions. The object, if any, will reside on the partition named by some ancestor of the absolute form of this pathname.
A file system may implement restrictions to certain operations on the actual file-system object, such as reading, writing, and executing. These restrictions are collectively known as access permissions. The file system may have multiple sets of access permissions on a single object. For example, one set may apply to the object's owner, and another may apply to all other users. The access permissions on an object may cause some methods in this class to fail.
Instances of the File class are immutable; that is, once
created, the abstract pathname represented by a File object
will never change.
java.nio.file package The java.nio.file
package defines interfaces and classes for the Java virtual machine to access
files, file attributes, and file systems. This API may be used to overcome
many of the limitations of the java.io.File class.
The toPath method may be used to obtain a Path that uses the abstract path represented by a File object to
locate a file. The resulting Path may be used with the java. class to provide more efficient and extensive access to
additional file operations, file attributes, and I/O exceptions to help
diagnose errors when an operation on a file fails.
| Modifier and Type | Class and Description |
|---|---|
| private static enum | File.
Enum type that indicates the status of a file path. |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private transient volatile Path | |
| private static final FileSystem | FS
The FileSystem object representing the platform's local file system. |
| private final String | path
This abstract pathname's normalized pathname string. |
| private static final long | |
| public static final String | pathSeparator
The system-dependent path-separator character, represented as a string for convenience. |
| public static final char | pathSeparatorChar
The system-dependent path-separator character. |
| private static final long | |
| private final transient int | prefixLength
The length of this abstract pathname's prefix, or zero if it has no prefix. |
| public static final String | separator
The system-dependent default name-separator character, represented as a string for convenience. |
| public static final char | separatorChar
The system-dependent default name-separator character. |
| private static final long | serialVersionUID
use serialVersionUID from JDK 1.0.2 for interoperability |
| private transient File. | status
The flag indicating whether the file path is invalid. |
| private static final Unsafe |
| Access | Constructor and Description |
|---|---|
| private | |
| private | |
| public | |
| public | |
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: true if and only if the abstract pathname exists
and the application is allowed to execute the fileTests whether the application can execute the file located by this abstract pathname. |
| public boolean | Returns: true if and only if the file located by this
abstract pathname exists and can be read by the
application; false otherwiseTests whether the application can read the file located by this abstract pathname. |
| public boolean | Returns: true if and only if the file system actually
contains a file located by this abstract pathname and
the application is allowed to write to the file;
false otherwise.Tests whether the application can modify the file located by this abstract pathname. |
| public int | Returns: Zero if the argument is equal to this abstract pathname, a value less than zero if this abstract pathname is lexicographically less than the argument, or a value greater than zero if this abstract pathname is lexicographically greater than the argumentThe abstract pathname to be compared to this abstract
pathname pathname)Implements java. Compares two abstract pathnames lexicographically. |
| public boolean | Returns: true if the named file does not exist and was
successfully created; false if the named file
already exists, including if it is a symbolic linkAtomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. |
| public static File | Returns: An abstract pathname locating a newly-created empty fileThe prefix string to be used in generating the file's
name; must be at least three characters long prefix, String The suffix string to be used in generating the file's
name; may be suffix, File null, in which case the
suffix ".tmp" will be usedThe directory in which the file is to be created, or
directory)null if the default temporary-file
directory is to be usedCreates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. |
| public static File | Returns: An abstract pathname locating a newly-created empty fileThe prefix string to be used in generating the file's
name; must be at least three characters long prefix, String The suffix string to be used in generating the file's
name; may be suffix)null, in which case the
suffix ".tmp" will be usedCreates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. |
| public boolean | Returns: true if and only if the file or directory is
successfully deleted; false otherwiseDeletes the file or directory located by this abstract pathname. |
| public void | deleteOnExit()
Requests that the file or directory located by this abstract pathname be deleted when the virtual machine terminates. |
| public boolean | Returns: true if and only if the objects are the same;
false otherwiseThe object to be compared with this abstract pathname obj)Overrides java. Tests this abstract pathname for equality with the given object. |
| public boolean | Returns: true if and only if the file or directory located
by this abstract pathname exists; false otherwiseTests whether the file or directory located by this abstract pathname exists. |
| public File | Returns: The absolute abstract pathname denoting the same file or directory as this abstract pathnameReturns the absolute form of this abstract pathname. |
| public String | Returns: The absolute pathname string denoting the same file or directory as this abstract pathnameReturns the absolute pathname string of this abstract pathname. |
| public File | Returns: The canonical pathname string locating the same file or directory as this abstract pathnameReturns the canonical form of this abstract pathname. |
| public String | Returns: The canonical pathname string locating the same file or directory as this abstract pathnameReturns the canonical pathname string of this abstract pathname. |
| public long | Returns: The number of unallocated bytes on the partition or0L
if the abstract pathname does not name a partition or if this
number cannot be obtained. This value will be less than or
equal to the total file system size returned by
getTotalSpace.Returns the number of unallocated bytes in the partition named by this abstract path name. |
| public String | Returns: The name component of this abstract pathname, or the empty string if this pathname's name sequence is emptyReturns the name component of this abstract pathname. |
| public String | Returns: The pathname string of the parent directory named by this abstract pathname, ornull if this pathname
does not name a parentReturns the pathname string of this abstract pathname's parent, or
|
| public File | Returns: The abstract pathname of the parent directory named by this abstract pathname, ornull if this pathname
does not name a parentReturns the abstract pathname of this abstract pathname's parent,
or |
| public String | Returns: The string form of this abstract pathnameConverts this abstract pathname into a pathname string. |
| pack-priv int | |
| public long | Returns: The size, in bytes, of the partition or0L if this
abstract pathname does not name a partition or if the size
cannot be obtainedReturns the size of the partition named by this abstract pathname. |
| public long | Returns: The number of available bytes on the partition or0L
if the abstract pathname does not name a partition or if this
number cannot be obtained. On systems where this information
is not available, this method will be equivalent to a call to
getFreeSpace.Returns the number of bytes available to this virtual machine on the partition named by this abstract pathname. |
| public int | Returns: A hash code for this abstract pathnameOverrides java. Computes a hash code for this abstract pathname. |
| public boolean | Returns: true if this abstract pathname is absolute,
false otherwiseTests whether this abstract pathname is absolute. |
| public boolean | Returns: true if and only if the file located by this
abstract pathname exists and is a directory;
false otherwiseTests whether the file located by this abstract pathname is a directory. |
| public boolean | Returns: true if and only if the file located by this
abstract pathname exists and is a normal file;
false otherwiseTests whether the file located by this abstract pathname is a normal file. |
| public boolean | Returns: true if and only if the file located by this
abstract pathname is hidden according to the conventions of the
underlying platformTests whether the file located by this abstract pathname is a hidden file. |
| pack-priv final boolean | |
| public long | Returns: Along value representing the time the file was
last modified, measured in milliseconds since the epoch
(00:00:00 GMT, January 1, 1970), or 0L if the
file does not exist or if an I/O error occurs. The value may
be negative indicating the number of milliseconds before the
epochReturns the time that the file located by this abstract pathname was last modified. |
| public long | Returns: The length, in bytes, of the file located by this abstract pathname, or0L if the file does not exist. Some
operating systems may return 0L for pathnames
locating system-dependent entities such as devices or pipes.Returns the length of the file located by this abstract pathname. |
| public String[] | Returns: An array of strings naming the files and directories in the directory located by this abstract pathname. The array will be empty if the directory is empty. Returnsnull if
this abstract pathname does not locate a directory, or if an
I/O error occurs.Returns an array of strings naming the files and directories in the directory located by this abstract pathname. |
| public String[] | Returns: An array of strings naming the files and directories in the directory located by this abstract pathname that were accepted by the givenfilter. The array will be empty if the
directory is empty or if no names were accepted by the filter.
Returns null if this abstract pathname does not locate
a directory, or if an I/O error occurs.A filename filter filter)Returns an array of strings naming the files and directories in the directory located by this abstract pathname that satisfy the specified filter. |
| public File[] | Returns: An array of abstract pathnames locating the files and directories in the directory located by this abstract pathname. The array will be empty if the directory is empty. Returnsnull if this abstract pathname does not locate a
directory, or if an I/O error occurs.Returns an array of abstract pathnames locating the files in the directory located by this abstract pathname. |
| public File[] | Returns: An array of abstract pathnames locating the files and directories in the directory located by this abstract pathname. The array will be empty if the directory is empty. Returnsnull if this abstract pathname does not locate a
directory, or if an I/O error occurs.A filename filter filter)Returns an array of abstract pathnames locating the files and directories in the directory located by this abstract pathname that satisfy the specified filter. |
| public File[] | Returns: An array of abstract pathnames locating the files and directories in the directory located by this abstract pathname. The array will be empty if the directory is empty. Returnsnull if this abstract pathname does not locate a
directory, or if an I/O error occurs.A file filter filter)Returns an array of abstract pathnames locating the files and directories in the directory located by this abstract pathname that satisfy the specified filter. |
| public static File[] | Returns: An array ofFile objects denoting the available
filesystem roots, or null if the set of roots could not
be determined. The array will be empty if there are no
filesystem roots.List the available filesystem roots. |
| public boolean | Returns: true if and only if the directory was
created; false otherwiseCreates the directory named by this abstract pathname. |
| public boolean | Returns: true if and only if the directory was created,
along with all necessary parent directories; false
otherwiseCreates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. |
| private final String[] | Returns: An array of strings naming the files and directories in the directory located by this abstract pathname. The array will be empty if the directory is empty. Returnsnull if
this abstract pathname does not locate a directory, or if an
I/O error occurs.Returns an array of strings naming the files and directories in the directory located by this abstract pathname. |
| private synchronized void | readObject(ObjectInputStream
the s)ObjectInputStream from which data is readreadObject is called to restore this filename. |
| public boolean | |
| public boolean | Returns: true if the operation succeeds, false if it
fails, or the value of the executable parameter if
setting the execute permission is not supported.If executable, boolean true, sets the access permission to allow execute
operations; if false to disallow execute operationsIf ownerOnly)true, the execute permission applies only to the
owner's execute permission; otherwise, it applies to everybody.
If the underlying file system can not distinguish the owner's
execute permission from that of others, then the permission will
apply to everybody, regardless of this value.Sets the owner's or everybody's execute permission for the file or directory located by this abstract pathname. |
| public boolean | Returns: true if the operation succeeds, false if it
fails, or the value of the executable parameter if
setting the execute permission is not supported.If executable)true, sets the access permission to allow execute
operations; if false to disallow execute operationsA convenience method to set the owner's execute permission for the file or directory located by this abstract pathname. |
| public boolean | Returns: true if and only if the operation succeeded;
false otherwiseThe new last-modified time, measured in milliseconds since
the epoch (00:00:00 GMT, January 1, 1970) time)Sets the last-modified time of the file or directory located by this abstract pathname. |
| public boolean | Returns: true if the operation succeeds, false if it
fails, or the value of the readable parameter if
setting the read permission is not supported.If readable, boolean true, sets the access permission to allow read
operations; if false to disallow read operationsIf ownerOnly)true, the read permission applies only to the
owner's read permission; otherwise, it applies to everybody. If
the underlying file system can not distinguish the owner's read
permission from that of others, then the permission will apply to
everybody, regardless of this value.Sets the owner's or everybody's read permission for the file or directory located by this abstract pathname. |
| public boolean | Returns: true if the operation succeeds, false if it
fails, or the value of the readable parameter if
setting the read permission is not supported.If readable)true, sets the access permission to allow read
operations; if false to disallow read operationsA convenience method to set the owner's read permission for the file or directory located by this abstract pathname. |
| public boolean | Returns: true if and only if the operation succeeded;
false otherwiseMarks the file or directory located by this abstract pathname so that only read operations are allowed. |
| public boolean | Returns: true if and only if the operation succeeded. The
operation will fail if the user does not have permission to change
the access permissions of this abstract pathname.If writable, boolean true, sets the access permission to allow write
operations; if false to disallow write operationsIf ownerOnly)true, the write permission applies only to the
owner's write permission; otherwise, it applies to everybody. If
the underlying file system can not distinguish the owner's write
permission from that of others, then the permission will apply to
everybody, regardless of this value.Sets the owner's or everybody's write permission of the file or directory located by this abstract pathname. |
| public boolean | Returns: true if and only if the operation succeeded. The
operation will fail if the user does not have permission to
change the access permissions of this abstract pathname.If writable)true, sets the access permission to allow write
operations; if false to disallow write operationsA convenience method to set the owner's write permission for the file or directory located by this abstract pathname. |
| private static String | |
| public Path | Returns: aPath constructed from this abstract pathReturns a |
| public String | Returns: The string form of this abstract pathnameOverrides java. Returns the pathname string of this abstract pathname. |
| public URI | Returns: An absolute, hierarchical URI with a scheme equal to"file", a path representing this abstract pathname,
and undefined authority, query, and fragment componentsConstructs a |
| public URL | Returns: A URL object representing the equivalent file URL
Deprecated
This method does not automatically escape characters that
are illegal in URLs.
Converts this abstract pathname into a |
| private synchronized void | writeObject(ObjectOutputStream
the s)ObjectOutputStream to which data is writtenWriteObject is called to save this filename. |