public class File
extends java.io.File
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.
Serializable,
Comparable,
Serialized Form| Constructor and Description |
|---|
File(java.io.File dir,
java.lang.String name)
Constructs a new file using the specified directory and name.
|
File(java.lang.String path)
Constructs a new file using the specified path.
|
File(java.lang.String dirPath,
java.lang.String name)
Constructs a new File using the specified directory path and file name,
placing a path separator between the two.
|
File(java.net.URI uri)
Constructs a new File using the path of the specified URI.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
canExecute()
Tests whether or not this process is allowed to execute this file.
|
boolean |
canRead()
Indicates whether the current context is allowed to read from this file.
|
boolean |
canWrite()
Indicates whether the current context is allowed to write to this file.
|
int |
compareTo(File another)
Returns the relative sort ordering of the paths for this file and the
file
another. |
boolean |
createNewFile()
Creates a new, empty file on the file system according to the path
information stored in this file.
|
static File |
createTempFile(java.lang.String prefix,
java.lang.String suffix)
Creates an empty temporary file using the given prefix and suffix as part
of the file name.
|
static File |
createTempFile(java.lang.String prefix,
java.lang.String suffix,
File directory)
Creates an empty temporary file in the given directory using the given
prefix and suffix as part of the file name.
|
boolean |
delete()
Deletes this file.
|
void |
deleteOnExit()
Schedules this file to be automatically deleted when the VM terminates
normally.
|
boolean |
equals(java.lang.Object obj)
Compares
obj to this file and returns true if they
represent the same object using a path specific comparison. |
boolean |
exists()
Returns a boolean indicating whether this file can be found on the
underlying file system.
|
File |
getAbsoluteFile()
Returns a new file constructed using the absolute path of this file.
|
java.lang.String |
getAbsolutePath()
Returns the absolute path of this file.
|
File |
getCanonicalFile()
Returns a new file created using the canonical path of this file.
|
java.lang.String |
getCanonicalPath()
Returns the canonical path of this file.
|
long |
getFreeSpace()
Returns the number of free bytes on the partition containing this path.
|
java.lang.String |
getName()
Returns the name of the file or directory represented by this file.
|
java.lang.String |
getParent()
Returns the pathname of the parent of this file.
|
File |
getParentFile()
Returns a new file made from the pathname of the parent of this file.
|
java.lang.String |
getPath()
Returns the path of this file.
|
long |
getTotalSpace()
Returns the total size in bytes of the partition containing this path.
|
long |
getUsableSpace()
Returns the number of usable free bytes on the partition containing this
path.
|
int |
hashCode()
Returns an integer hash code for the receiver.
|
boolean |
isAbsolute()
Indicates if this file's pathname is absolute.
|
boolean |
isDirectory()
Indicates if this file represents a directory on the underlying
file system.
|
boolean |
isFile()
Indicates if this file represents a file on the underlying file
system.
|
boolean |
isHidden()
Returns whether or not this file is a hidden file as defined by the
operating system.
|
long |
lastModified()
Returns the time when this file was last modified, measured in
milliseconds since January 1st, 1970, midnight.
|
long |
length()
Returns the length of this file in bytes.
|
java.lang.String[] |
list()
Returns an array of strings with the file names in the directory
represented by this file.
|
java.lang.String[] |
list(FilenameFilter filter)
Gets a list of the files in the directory represented by this file.
|
File[] |
listFiles()
Returns an array of files contained in the directory represented by this
file.
|
File[] |
listFiles(FileFilter filter)
Gets a list of the files in the directory represented by this file.
|
File[] |
listFiles(FilenameFilter filter)
Gets a list of the files in the directory represented by this file.
|
boolean |
mkdir()
Creates the directory named by the trailing filename of this file.
|
boolean |
mkdirs()
Creates the directory named by the trailing filename of this file,
including the complete directory path required to create this directory.
|
boolean |
renameTo(File newPath)
Renames this file to
newPath. |
boolean |
setExecutable(boolean executable)
Equivalent to setExecutable(executable, true).
|
boolean |
setExecutable(boolean executable,
boolean ownerOnly)
Manipulates the execute permissions for the abstract path designated by
this file.
|
boolean |
setLastModified(long time)
Sets the time this file was last modified, measured in milliseconds since
January 1st, 1970, midnight.
|
boolean |
setReadable(boolean readable)
Equivalent to setReadable(readable, true).
|
boolean |
setReadable(boolean readable,
boolean ownerOnly)
Manipulates the read permissions for the abstract path designated by this
file.
|
boolean |
setReadOnly()
Equivalent to setWritable(false, false).
|
boolean |
setWritable(boolean writable)
Equivalent to setWritable(writable, true).
|
boolean |
setWritable(boolean writable,
boolean ownerOnly)
Manipulates the write permissions for the abstract path designated by
this file.
|
java.lang.String |
toString()
Returns a string containing a concise, human-readable description of this
file.
|
java.net.URI |
toURI()
Returns a Uniform Resource Identifier for this file.
|
java.net.URL |
toURL()
Deprecated.
use
toURI() and URI.toURL() to get
correct escaping of illegal characters. |
public File(java.io.File dir,
java.lang.String name)
dir - the directory where the file is stored.name - the file's name.java.lang.NullPointerException - if name is null.public File(java.lang.String path)
path - the path to be used for the file.public File(java.lang.String dirPath,
java.lang.String name)
dirPath - the path to the directory where the file is stored.name - the file's name.java.lang.NullPointerException - if name == null.public File(java.net.URI 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.java.lang.IllegalArgumentException - if uri does not comply with the
conditions above.toURI(),
URIpublic boolean canExecute()
canExecute in class java.io.Filetrue if this file can be executed, false
otherwise.public boolean canRead()
canRead in class java.io.Filetrue if this file can be read, false otherwise.public boolean canWrite()
canWrite in class java.io.Filetrue if this file can be written, false
otherwise.public int compareTo(File another)
another. The ordering is platform dependent.another - a file to compare this file toComparablepublic boolean delete()
Note that this method does not throw IOException on
failure. Callers must check the return value.
delete in class java.io.Filetrue if this file was deleted, false otherwise.public void deleteOnExit()
throws java.lang.UnsupportedOperationException
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().
deleteOnExit in class java.io.Filejava.lang.UnsupportedOperationExceptionpublic boolean equals(java.lang.Object obj)
obj to this file and returns true if they
represent the same object using a path specific comparison.equals in class java.io.Fileobj - the object to compare this file with.true if obj is the same as this object,
false otherwise.public boolean exists()
exists in class java.io.Filetrue if this file exists, false otherwise.public java.lang.String getAbsolutePath()
/.
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.
getAbsolutePath in class java.io.Filepublic File getAbsoluteFile()
new File(this.getAbsolutePath()).getAbsoluteFile in class java.io.Filepublic java.lang.String getCanonicalPath()
throws java.io.IOException
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.
getCanonicalPath in class java.io.Filejava.io.IOException - if an I/O error occurs.public File getCanonicalFile() throws java.io.IOException
new File(this.getCanonicalPath()).getCanonicalFile in class java.io.Filejava.io.IOException - if an I/O error occurs.public java.lang.String getName()
getName in class java.io.Filepublic java.lang.String getParent()
null is returned if there is no
parent.getParent in class java.io.Filenull.public File getParentFile()
null is
returned when there is no parent.getParentFile in class java.io.Filenull.public java.lang.String getPath()
getPath in class java.io.Filepublic int hashCode()
equals returns true must return the same hash code.hashCode in class java.io.Fileequals(java.lang.Object)public boolean isAbsolute()
isAbsolute in class java.io.Filetrue if this file's pathname is absolute, false
otherwise.getPath()public boolean isDirectory()
isDirectory in class java.io.Filetrue if this file is a directory, false
otherwise.public boolean isFile()
isFile in class java.io.Filetrue if this file is a file, false otherwise.public boolean isHidden()
isHidden in class java.io.Filetrue if the file is hidden, false otherwise.public long lastModified()
lastModified in class java.io.Filepublic boolean setLastModified(long time)
Note that this method does not throw IOException on
failure. Callers must check the return value.
setLastModified in class java.io.Filetime - the last modification time for this file.true if the operation is successful, false
otherwise.java.lang.IllegalArgumentException - if time < 0.public boolean setReadOnly()
setReadOnly in class java.io.FilesetWritable(boolean, boolean)public boolean setExecutable(boolean executable,
boolean ownerOnly)
Note that this method does not throw IOException on
failure. Callers must check the return value.
setExecutable in class java.io.Fileexecutable - To allow execute permission if true, otherwise disallowownerOnly - 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.public boolean setExecutable(boolean executable)
setExecutable in class java.io.FilesetExecutable(boolean, boolean)public boolean setReadable(boolean readable,
boolean ownerOnly)
setReadable in class java.io.Filereadable - To allow read permission if true, otherwise disallowownerOnly - 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.public boolean setReadable(boolean readable)
setReadable in class java.io.FilesetReadable(boolean, boolean)public boolean setWritable(boolean writable,
boolean ownerOnly)
setWritable in class java.io.Filewritable - To allow write permission if true, otherwise disallowownerOnly - 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.public boolean setWritable(boolean writable)
setWritable in class java.io.FilesetWritable(boolean, boolean)public long length()
length in class java.io.Filepublic java.lang.String[] list()
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.
list in class java.io.Filenull.public java.lang.String[] list(FilenameFilter filter)
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.public File[] listFiles()
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.listFiles in class java.io.Filenull.public File[] listFiles(FilenameFilter filter)
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.public File[] listFiles(FileFilter filter)
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.public boolean mkdir()
Note that this method does not throw IOException on
failure. Callers must check the return value.
mkdir in class java.io.Filetrue if the directory has been created, false
otherwise.mkdirs()public boolean mkdirs()
Note that this method does not throw IOException on
failure. Callers must check the return value.
mkdirs in class java.io.Filetrue if the necessary directories have been created,
false if the target directory already exists or one of
the directories can not be created.mkdir()public boolean createNewFile()
throws java.io.IOException
This method is not generally useful. For creating temporary files, use
createTempFile(java.lang.String, java.lang.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().
createNewFile in class java.io.Filejava.io.IOException - if it's not possible to create the file.public static File createTempFile(java.lang.String prefix, java.lang.String suffix) throws java.io.IOException
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.java.io.IOException - if an error occurs when writing the file.public static File createTempFile(java.lang.String prefix, java.lang.String suffix, File directory) throws java.io.IOException
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.java.lang.IllegalArgumentException - if the length of prefix is less
than 3.java.io.IOException - if an error occurs when writing the file.public boolean renameTo(File newPath)
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.public java.lang.String toString()
toString in class java.io.Filepublic java.net.URI toURI()
toURI in class java.io.File@Deprecated
public java.net.URL toURL()
throws java.net.MalformedURLException
toURI() and URI.toURL() to get
correct escaping of illegal characters.toURL in class java.io.Filejava.net.MalformedURLException - if the path cannot be transformed
into a URL.public long getTotalSpace()
getTotalSpace in class java.io.Filepublic long getUsableSpace()
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.)
getUsableSpace in class java.io.Filepublic long getFreeSpace()
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.
getFreeSpace in class java.io.File