java.lang.Object | |
↳ | java.io.File |
An "abstract" representation of a file system entity identified by a pathname. The pathname may be absolute (relative to the root directory of the file system) or relative to the current directory in which the program is running.
The actual file referenced by a File
may or may not exist. It may
also, despite the name File
, be a directory or other non-regular
file.
This class provides limited functionality for getting/setting file permissions, file type, and last modified time.
On Android strings are converted to UTF-8 byte sequences when sending filenames to
the operating system, and byte sequences returned by the operating system (from the
various list
methods) are converted to strings by decoding them as UTF-8
byte sequences.
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
pathSeparator | The system-dependent string used to separate components in search paths (":"). | ||||||||||
pathSeparatorChar | The system-dependent character used to separate components in search paths (':'). | ||||||||||
separator | The system-dependent string used to separate components in filenames ('/'). | ||||||||||
separatorChar | The system-dependent character used to separate components in filenames ('/'). |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new file using the specified directory and name.
| |||||||||||
Constructs a new file using the specified path.
| |||||||||||
Constructs a new File using the specified directory path and file name,
placing a path separator between the two.
| |||||||||||
Constructs a new File using the path of the specified URI.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Tests whether or not this process is allowed to execute this file.
| |||||||||||
Indicates whether the current context is allowed to read from this file.
| |||||||||||
Indicates whether the current context is allowed to write to this file.
| |||||||||||
Returns the relative sort ordering of the paths for this file and the
file
another . | |||||||||||
Creates a new, empty file on the file system according to the path
information stored in this file.
| |||||||||||
Creates an empty temporary file in the given directory using the given
prefix and suffix as part of the file name.
| |||||||||||
Creates an empty temporary file using the given prefix and suffix as part
of the file name.
| |||||||||||
Deletes this file.
| |||||||||||
Schedules this file to be automatically deleted when the VM terminates normally.
| |||||||||||
Compares
obj to this file and returns true if they
represent the same object using a path specific comparison. | |||||||||||
Returns a boolean indicating whether this file can be found on the
underlying file system.
| |||||||||||
Returns a new file constructed using the absolute path of this file.
| |||||||||||
Returns the absolute path of this file.
| |||||||||||
Returns a new file created using the canonical path of this file.
| |||||||||||
Returns the canonical path of this file.
| |||||||||||
Returns the number of free bytes on the partition containing this path.
| |||||||||||
Returns the name of the file or directory represented by this file.
| |||||||||||
Returns the pathname of the parent of this file.
| |||||||||||
Returns a new file made from the pathname of the parent of this file.
| |||||||||||
Returns the path of this file.
| |||||||||||
Returns the total size in bytes of the partition containing this path.
| |||||||||||
Returns the number of usable free bytes on the partition containing this path.
| |||||||||||
Returns an integer hash code for the receiver.
| |||||||||||
Indicates if this file's pathname is absolute.
| |||||||||||
Indicates if this file represents a directory on the
underlying file system.
| |||||||||||
Indicates if this file represents a file on the underlying
file system.
| |||||||||||
Returns whether or not this file is a hidden file as defined by the
operating system.
| |||||||||||
Returns the time when this file was last modified, measured in
milliseconds since January 1st, 1970, midnight.
| |||||||||||
Returns the length of this file in bytes.
| |||||||||||
Returns an array of strings with the file names in the directory
represented by this file.
| |||||||||||
Gets a list of the files in the directory represented by this file.
| |||||||||||
Returns an array of files contained in the directory represented by this
file.
| |||||||||||
Gets a list of the files in the directory represented by this file.
| |||||||||||
Gets a list of the files in the directory represented by this file.
| |||||||||||
Returns the file system roots.
| |||||||||||
Creates the directory named by the trailing filename of this file.
| |||||||||||
Creates the directory named by the trailing filename of this file,
including the complete directory path required to create this directory.
| |||||||||||
Renames this file to
newPath . | |||||||||||
Equivalent to setExecutable(executable, true).
| |||||||||||
Manipulates the execute permissions for the abstract path designated by
this file.
| |||||||||||
Sets the time this file was last modified, measured in milliseconds since
January 1st, 1970, midnight.
| |||||||||||
Equivalent to setWritable(false, false).
| |||||||||||
Equivalent to setReadable(readable, true).
| |||||||||||
Manipulates the read permissions for the abstract path designated by this
file.
| |||||||||||
Manipulates the write permissions for the abstract path designated by this
file.
| |||||||||||
Equivalent to setWritable(writable, true).
| |||||||||||
Returns a string containing a concise, human-readable description of this
file.
| |||||||||||
Returns a Uniform Resource Identifier for this file.
| |||||||||||
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
java.lang.Comparable
|
The system-dependent string used to separate components in search paths (":").
See pathSeparatorChar
.
The system-dependent character used to separate components in search paths (':'). This is used to split such things as the PATH environment variable and classpath system properties into lists of directories to be searched.
This field is initialized from the system property "path.separator". Later changes to that property will have no effect on this field or this class.
The system-dependent string used to separate components in filenames ('/').
See separatorChar
.
The system-dependent character used to separate components in filenames ('/'). Use of this (rather than hard-coding '/') helps portability to other operating systems.
This field is initialized from the system property "file.separator". Later changes to that property will have no effect on this field or this class.
Constructs a new file using the specified directory and name.
dir | the directory where the file is stored. |
---|---|
name | the file's name. |
NullPointerException | if name is null .
|
---|
Constructs a new file using the specified path.
path | the path to be used for the file. |
---|
Constructs a new File using the specified directory path and file name, placing a path separator between the two.
dirPath | the path to the directory where the file is stored. |
---|---|
name | the file's name. |
NullPointerException | if name == null .
|
---|
Constructs a new File using the path of the specified URI. uri
needs to be an absolute and hierarchical Unified Resource Identifier with
file scheme and non-empty path component, but with undefined authority,
query or fragment components.
uri | the Unified Resource Identifier that is used to construct this file. |
---|
IllegalArgumentException | if uri does not comply with the conditions above. |
---|
Tests whether or not this process is allowed to execute this file. Note that this is a best-effort result; the only way to be certain is to actually attempt the operation.
true
if this file can be executed, false
otherwise.Indicates whether the current context is allowed to read from this file.
true
if this file can be read, false
otherwise.
Indicates whether the current context is allowed to write to this file.
true
if this file can be written, false
otherwise.
Returns the relative sort ordering of the paths for this file and the
file another
. The ordering is platform dependent.
another | a file to compare this file to |
---|
Creates a new, empty file on the file system according to the path information stored in this file. This method returns true if it creates a file, false if the file already existed. Note that it returns false even if the file is not a file (because it's a directory, say).
This method is not generally useful. For creating temporary files,
use createTempFile(String, String)
instead. For reading/writing files, use FileInputStream
,
FileOutputStream
, or RandomAccessFile
, all of which can create files.
Note that this method does not throw IOException
if the file
already exists, even if it's not a regular file. Callers should always check the
return value, and may additionally want to call isFile()
.
IOException | if it's not possible to create the file. |
---|
Creates an empty temporary file in the given directory using the given
prefix and suffix as part of the file name. If suffix
is null, .tmp
is used.
Note that this method does not call deleteOnExit()
, but see the
documentation for that method before you call it manually.
prefix | the prefix to the temp file name. |
---|---|
suffix | the suffix to the temp file name. |
directory | the location to which the temp file is to be written, or
null for the default location for temporary files,
which is taken from the "java.io.tmpdir" system property. It
may be necessary to set this property to an existing, writable
directory for this method to work properly. |
IllegalArgumentException | if the length of prefix is less than 3. |
---|---|
IOException | if an error occurs when writing the file. |
Creates an empty temporary file using the given prefix and suffix as part
of the file name. If suffix
is null, .tmp
is used. This
method is a convenience method that calls
createTempFile(String, String, File)
with the third argument
being null
.
prefix | the prefix to the temp file name. |
---|---|
suffix | the suffix to the temp file name. |
IOException | if an error occurs when writing the file. |
---|
Deletes this file. Directories must be empty before they will be deleted.
Note that this method does not throw IOException
on failure.
Callers must check the return value.
true
if this file was deleted, false
otherwise.
Schedules this file to be automatically deleted when the VM terminates normally.
Note that on Android, the application lifecycle does not include VM termination, so calling this method will not ensure that files are deleted. Instead, you should use the most appropriate out of:
finally
clause to manually invoke delete()
.
Compares obj
to this file and returns true
if they
represent the same object using a path specific comparison.
obj | the object to compare this file with. |
---|
true
if obj
is the same as this object,
false
otherwise.
Returns a boolean indicating whether this file can be found on the underlying file system.
true
if this file exists, false
otherwise.
Returns a new file constructed using the absolute path of this file.
Equivalent to new File(this.getAbsolutePath())
.
Returns the absolute path of this file. An absolute path is a path that starts at a root
of the file system. On Android, there is only one root: /
.
A common use for absolute paths is when passing paths to a Process
as
command-line arguments, to remove the requirement implied by relative paths, that the
child must have the same working directory as its parent.
Returns a new file created using the canonical path of this file.
Equivalent to new File(this.getCanonicalPath())
.
IOException | if an I/O error occurs. |
---|
Returns the canonical path of this file. An absolute path is one that begins at the root of the file system. A canonical path is an absolute path with symbolic links and references to "." or ".." resolved. If a path element does not exist (or is not searchable), there is a conflict between interpreting canonicalization as a textual operation (where "a/../b" is "b" even if "a" does not exist) .
Most callers should use getAbsolutePath()
instead. A canonical path is
significantly more expensive to compute, and not generally useful. The primary
use for canonical paths is determining whether two paths point to the same file by
comparing the canonicalized paths.
It can be actively harmful to use a canonical path, specifically because canonicalization removes symbolic links. It's wise to assume that a symbolic link is present for a reason, and that that reason is because the link may need to change. Canonicalization removes this layer of indirection. Good code should generally avoid caching canonical paths.
IOException | if an I/O error occurs. |
---|
Returns the number of free bytes on the partition containing this path. Returns 0 if this path does not exist.
Note that this is likely to be an optimistic over-estimate and should not be taken as a guarantee your application can actually write this many bytes.
Returns the name of the file or directory represented by this file.
Returns the pathname of the parent of this file. This is the path up to
but not including the last name. null
is returned if there is no
parent.
null
.
Returns a new file made from the pathname of the parent of this file.
This is the path up to but not including the last name. null
is
returned when there is no parent.
null
.
Returns the path of this file.
Returns the total size in bytes of the partition containing this path. Returns 0 if this path does not exist.
Returns the number of usable free bytes on the partition containing this path. Returns 0 if this path does not exist.
Note that this is likely to be an optimistic over-estimate and should not
be taken as a guarantee your application can actually write this many bytes.
On Android (and other Unix-based systems), this method returns the number of free bytes
available to non-root users, regardless of whether you're actually running as root,
and regardless of any quota or other restrictions that might apply to the user.
(The getFreeSpace
method returns the number of bytes potentially available to root.)
Returns an integer hash code for the receiver. Any two objects for which
equals
returns true
must return the same hash code.
Indicates if this file's pathname is absolute. Whether a pathname is absolute is platform specific. On Android, absolute paths start with the character '/'.
true
if this file's pathname is absolute, false
otherwise.Indicates if this file represents a directory on the underlying file system.
true
if this file is a directory, false
otherwise.
Indicates if this file represents a file on the underlying file system.
true
if this file is a file, false
otherwise.
Returns whether or not this file is a hidden file as defined by the operating system. The notion of "hidden" is system-dependent. For Unix systems a file is considered hidden if its name starts with a ".". For Windows systems there is an explicit flag in the file system for this purpose.
true
if the file is hidden, false
otherwise.
Returns the time when this file was last modified, measured in milliseconds since January 1st, 1970, midnight. Returns 0 if the file does not exist.
Returns the length of this file in bytes. Returns 0 if the file does not exist. The result for a directory is not defined.
Returns an array of strings with the file names in the directory
represented by this file. The result is null
if this file is not
a directory.
The entries .
and ..
representing the current and parent
directory are not returned as part of the list.
null
.
Gets a list of the files in the directory represented by this file. This
list is then filtered through a FilenameFilter and the names of files
with matching names are returned as an array of strings. Returns
null
if this file is not a directory. If filter
is
null
then all filenames match.
The entries .
and ..
representing the current and parent
directories are not returned as part of the list.
filter | the filter to match names against, may be null . |
---|
null
.
Returns an array of files contained in the directory represented by this
file. The result is null
if this file is not a directory. The
paths of the files in the array are absolute if the path of this file is
absolute, they are relative otherwise.
null
.
Gets a list of the files in the directory represented by this file. This
list is then filtered through a FilenameFilter and files with matching
names are returned as an array of files. Returns null
if this
file is not a directory. If filter
is null
then all
filenames match.
The entries .
and ..
representing the current and parent
directories are not returned as part of the list.
filter | the filter to match names against, may be null . |
---|
null
.
Gets a list of the files in the directory represented by this file. This
list is then filtered through a FileFilter and matching files are
returned as an array of files. Returns null
if this file is not a
directory. If filter
is null
then all files match.
The entries .
and ..
representing the current and parent
directories are not returned as part of the list.
filter | the filter to match names against, may be null . |
---|
null
.
Returns the file system roots. On Android and other Unix systems, there is
a single root, /
.
Creates the directory named by the trailing filename of this file. Does not create the complete path required to create this directory.
Note that this method does not throw IOException
on failure.
Callers must check the return value.
true
if the directory has been created, false
otherwise.Creates the directory named by the trailing filename of this file, including the complete directory path required to create this directory.
Note that this method does not throw IOException
on failure.
Callers must check the return value.
true
if the necessary directories have been created,
false
if the target directory already exists or one of
the directories can not be created.Renames this file to newPath
. This operation is supported for both
files and directories.
Many failures are possible. Some of the more likely failures include:
Note that this method does not throw IOException
on failure.
Callers must check the return value.
newPath | the new path. |
---|
Equivalent to setExecutable(executable, true).
Manipulates the execute permissions for the abstract path designated by this file.
Note that this method does not throw IOException
on failure.
Callers must check the return value.
executable | To allow execute permission if true, otherwise disallow |
---|---|
ownerOnly | To manipulate execute permission only for owner if true, otherwise for everyone. The manipulation will apply to everyone regardless of this value if the underlying system does not distinguish owner and other users. |
Sets the time this file was last modified, measured in milliseconds since January 1st, 1970, midnight.
Note that this method does not throw IOException
on failure.
Callers must check the return value.
time | the last modification time for this file. |
---|
true
if the operation is successful, false
otherwise.IllegalArgumentException | if time < 0 .
|
---|
Equivalent to setReadable(readable, true).
Manipulates the read permissions for the abstract path designated by this file.
readable | To allow read permission if true, otherwise disallow |
---|---|
ownerOnly | To manipulate read permission only for owner if true, otherwise for everyone. The manipulation will apply to everyone regardless of this value if the underlying system does not distinguish owner and other users. |
Manipulates the write permissions for the abstract path designated by this file.
writable | To allow write permission if true, otherwise disallow |
---|---|
ownerOnly | To manipulate write permission only for owner if true, otherwise for everyone. The manipulation will apply to everyone regardless of this value if the underlying system does not distinguish owner and other users. |
Equivalent to setWritable(writable, true).
Returns a string containing a concise, human-readable description of this file.
Returns a Uniform Resource Identifier for this file. The URI is system dependent and may not be transferable between different operating / file systems.
This method is deprecated.
use toURI()
and toURL()
to get
correct escaping of illegal characters.
Returns a Uniform Resource Locator for this file. The URL is system dependent and may not be transferable between different operating / file systems.
MalformedURLException | if the path cannot be transformed into a URL. |
---|