APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.io

public Class File

extends Object
implements Serializable, Comparable<File>
Class Inheritance
All Implemented Interfaces
java.lang.Comparable, java.io.Serializable
Imports
java.net.URI, .URL, .MalformedURLException, .URISyntaxException, java.nio.file.FileStore, .FileSystems, .Path, java.security.SecureRandom, java.util.ArrayList, .List, jdk.internal.util.StaticProperty

An abstract representation of file and directory pathnames.

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:

  1. An optional system-dependent prefix string, such as a disk-drive specifier, "/" for the UNIX root directory, or "\\\\" for a Microsoft Windows UNC pathname, and
  2. A sequence of zero or more string names.
The first name in an abstract pathname may be a directory name or, in the case of Microsoft Windows UNC pathnames, a hostname. Each subsequent name in an abstract pathname denotes a directory; the last name may denote either a directory or a file. The empty abstract pathname has no prefix and an empty name sequence. Accessing a file with the empty abstract pathname is equivalent to accessing the current user directory.

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:

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.

Interoperability with 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.nio.file.Files 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.

Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum
File.PathStatus

Enum type that indicates the status of a file path.

private static class

Field Summary

Modifier and TypeField 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.PathStatus
status

The flag indicating whether the file path is invalid.

private static final Unsafe

Constructor Summary

AccessConstructor and Description
private
File(String pathname, int prefixLength)

Internal constructor for already-normalized pathname strings.

private
File(String child, File parent)

Internal constructor for already-normalized pathname strings.

public
File(String
A pathname string
pathname
)

Creates a new File instance by converting the given pathname string into an abstract pathname.

public
File(String
The parent pathname string
parent
,
String
The child pathname string
child
)

Creates a new File instance from a parent pathname string and a child pathname string.

public
File(File
The parent abstract pathname
parent
,
String
The child pathname string
child
)

Creates a new File instance from a parent abstract pathname and a child pathname string.

public
File(URI
An absolute, hierarchical URI with a scheme equal to "file", a non-empty path component, and undefined authority, query, and fragment components
uri
)

Creates a new File instance by converting the given file: URI into an abstract pathname.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if and only if the abstract pathname exists and the application is allowed to execute the file
canExecute
()

Tests 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 otherwise
canRead
()

Tests 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.
canWrite
()

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 argument
compareTo
(File
The abstract pathname to be compared to this abstract pathname
pathname
)

Implements java.lang.Comparable.compareTo.

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 link
createNewFile
()

Atomically 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 file
createTempFile
(String
The 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 null, in which case the suffix ".tmp" will be used
suffix
,
File
The directory in which the file is to be created, or null if the default temporary-file directory is to be used
directory
)

Creates 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 file
createTempFile
(String
The 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 null, in which case the suffix ".tmp" will be used
suffix
)

Creates 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 otherwise
delete
()

Deletes 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 otherwise
equals
(Object
The object to be compared with this abstract pathname
obj
)

Overrides java.lang.Object.equals.

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 otherwise
exists
()

Tests 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 pathname
getAbsoluteFile
()

Returns the absolute form of this abstract pathname.

public String

Returns:

The absolute pathname string denoting the same file or directory as this abstract pathname
getAbsolutePath
()

Returns the absolute pathname string of this abstract pathname.

public File

Returns:

The canonical pathname string locating the same file or directory as this abstract pathname
getCanonicalFile
()

Returns the canonical form of this abstract pathname.

public String

Returns:

The canonical pathname string locating the same file or directory as this abstract pathname
getCanonicalPath
()

Returns the canonical pathname string of this abstract pathname.

public long

Returns:

The number of unallocated bytes on the partition or 0L 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.
getFreeSpace
()

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 empty
getName
()

Returns the name component of this abstract pathname.

public String

Returns:

The pathname string of the parent directory named by this abstract pathname, or null if this pathname does not name a parent
getParent
()

Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.

public File

Returns:

The abstract pathname of the parent directory named by this abstract pathname, or null if this pathname does not name a parent
getParentFile
()

Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent directory.

public String

Returns:

The string form of this abstract pathname
getPath
()

Converts this abstract pathname into a pathname string.

pack-priv int
getPrefixLength()

Returns the length of this abstract pathname's prefix.

public long

Returns:

The size, in bytes, of the partition or 0L if this abstract pathname does not name a partition or if the size cannot be obtained
getTotalSpace
()

Returns the size of the partition named by this abstract pathname.

public long

Returns:

The number of available bytes on the partition or 0L 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.
getUsableSpace
()

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 pathname
hashCode
()

Overrides java.lang.Object.hashCode.

Computes a hash code for this abstract pathname.

public boolean

Returns:

true if this abstract pathname is absolute, false otherwise
isAbsolute
()

Tests 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 otherwise
isDirectory
()

Tests 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 otherwise
isFile
()

Tests 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 platform
isHidden
()

Tests whether the file located by this abstract pathname is a hidden file.

pack-priv final boolean

Returns:

true if the file path is invalid.
isInvalid
()

Check if the file has an invalid path.

public long

Returns:

A long 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 epoch
lastModified
()

Returns 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, or 0L if the file does not exist. Some operating systems may return 0L for pathnames locating system-dependent entities such as devices or pipes.
length
()

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. Returns null if this abstract pathname does not locate a directory, or if an I/O error occurs.
list
()

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 given filter. 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.
list
(FilenameFilter
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. Returns null if this abstract pathname does not locate a directory, or if an I/O error occurs.
listFiles
()

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. Returns null if this abstract pathname does not locate a directory, or if an I/O error occurs.
listFiles
(FilenameFilter
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. Returns null if this abstract pathname does not locate a directory, or if an I/O error occurs.
listFiles
(FileFilter
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 of File 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.
listRoots
()

List the available filesystem roots.

public boolean

Returns:

true if and only if the directory was created; false otherwise
mkdir
()

Creates 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 otherwise
mkdirs
()

Creates 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. Returns null if this abstract pathname does not locate a directory, or if an I/O error occurs.
normalizedList
()

Returns an array of strings naming the files and directories in the directory located by this abstract pathname.

private synchronized void
readObject(ObjectInputStream
the ObjectInputStream from which data is read
s
)

readObject is called to restore this filename.

public boolean

Returns:

true if and only if the renaming succeeded; false otherwise
renameTo
(File
The new abstract pathname for the named file
dest
)

Renames the file 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.
setExecutable
(boolean
If true, sets the access permission to allow execute operations; if false to disallow execute operations
executable
,
boolean
If 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.
ownerOnly
)

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.
setExecutable
(boolean
If true, sets the access permission to allow execute operations; if false to disallow execute operations
executable
)

A 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 otherwise
setLastModified
(long
The 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.
setReadable
(boolean
If true, sets the access permission to allow read operations; if false to disallow read operations
readable
,
boolean
If 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.
ownerOnly
)

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.
setReadable
(boolean
If true, sets the access permission to allow read operations; if false to disallow read operations
readable
)

A 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 otherwise
setReadOnly
()

Marks 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.
setWritable
(boolean
If true, sets the access permission to allow write operations; if false to disallow write operations
writable
,
boolean
If 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.
ownerOnly
)

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.
setWritable
(boolean
If true, sets the access permission to allow write operations; if false to disallow write operations
writable
)

A convenience method to set the owner's write permission for the file or directory located by this abstract pathname.

private static String
slashify(String path, boolean isDirectory)

public Path

Returns:

a Path constructed from this abstract path
toPath
()

Returns a java.nio.file.Path object constructed from this abstract path.

public String

Returns:

The string form of this abstract pathname
toString
()

Overrides java.lang.Object.toString.

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 components
toURI
()

Constructs a file: URI that represents this abstract pathname.

public URL

Returns:

A URL object representing the equivalent file URL
toURL
()
Deprecated This method does not automatically escape characters that are illegal in URLs.

Converts this abstract pathname into a file: URL.

private synchronized void
writeObject(ObjectOutputStream
the ObjectOutputStream to which data is written
s
)

WriteObject is called to save this filename.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait