|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object![]()
![]()
![]()
java.io.File
, Comparable
<File
>public class File

, Comparable
<File
>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:
"/" 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 and separator

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.
separatorChar

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.
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 the File class are immutable; that is, once
created, the abstract pathname represented by a File object
will never change.
| Field Summary | |
|---|---|
static String |
pathSeparator
The system-dependent path-separator character, represented as a string for convenience. |
static char |
pathSeparatorChar
The system-dependent path-separator character. |
static String |
separator
The system-dependent default name-separator character, represented as a string for convenience. |
static char |
separatorChar
The system-dependent default name-separator character. |
| Constructor Summary | |
|---|---|
File
Creates a new File instance from a parent abstract
pathname and a child pathname string. |
|
File
Creates a new File instance by converting the given
pathname string into an abstract pathname. |
|
File
Creates a new File instance from a parent pathname string
and a child pathname string. |
|
File
Creates a new File instance by converting the given file: URI into an abstract pathname. |
|
| Method Summary | |
|---|---|
boolean |
canRead
Tests whether the application can read the file denoted by this abstract pathname. |
boolean |
canWrite
Tests whether the application can modify the file denoted by this abstract pathname. |
int |
compareTo
Compares two abstract pathnames lexicographically. |
boolean |
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. |
static File |
createTempFile
Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. |
static File |
createTempFile
Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. |
boolean |
delete
Deletes the file or directory denoted by this abstract pathname. |
void |
deleteOnExit
Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates. |
boolean |
equals
Tests this abstract pathname for equality with the given object. |
boolean |
exists
Tests whether the file or directory denoted by this abstract pathname exists. |
File |
getAbsoluteFile
Returns the absolute form of this abstract pathname. |
String |
getAbsolutePath
Returns the absolute pathname string of this abstract pathname. |
File |
getCanonicalFile
Returns the canonical form of this abstract pathname. |
String |
getCanonicalPath
Returns the canonical pathname string of this abstract pathname. |
String |
getName
Returns the name of the file or directory denoted by this abstract pathname. |
String |
getParent
Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory. |
File |
getParentFile
Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent
directory. |
String |
getPath
Converts this abstract pathname into a pathname string. |
int |
hashCode
Computes a hash code for this abstract pathname. |
boolean |
isAbsolute
Tests whether this abstract pathname is absolute. |
boolean |
isDirectory
Tests whether the file denoted by this abstract pathname is a directory. |
boolean |
isFile
Tests whether the file denoted by this abstract pathname is a normal file. |
boolean |
isHidden
Tests whether the file named by this abstract pathname is a hidden file. |
long |
lastModified
Returns the time that the file denoted by this abstract pathname was last modified. |
long |
length
Returns the length of the file denoted by this abstract pathname. |
String |
list
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname. |
String |
list
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. |
File |
listFiles
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. |
File |
listFiles
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. |
File |
listFiles
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter. |
static File |
listRoots
List the available filesystem roots. |
boolean |
mkdir
Creates the directory named by this abstract pathname. |
boolean |
mkdirs
Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. |
boolean |
renameTo
Renames the file denoted by this abstract pathname. |
boolean |
setLastModified
Sets the last-modified time of the file or directory named by this abstract pathname. |
boolean |
setReadOnly
Marks the file or directory named by this abstract pathname so that only read operations are allowed. |
String |
toString
Returns the pathname string of this abstract pathname. |
URI |
toURI
Constructs a file: URI that represents this abstract pathname. |
URL |
toURL
Converts this abstract pathname into a file: URL. |
Methods inherited from class java.lang.Object ![]() |
|---|
clone |
| Field Detail |
|---|

public static final char separatorChar
file.separator. On UNIX systems the value of this
field is '/'; on Microsoft Windows systems it is '\\'.
System.getProperty(java.lang.String)


public static final String![]()
![]()
separator
separatorChar

.

public static final char pathSeparatorChar
path.separator. This character is used to
separate filenames in a sequence of files given as a path list.
On UNIX systems, this character is ':'; on Microsoft Windows systems it
is ';'.
System.getProperty(java.lang.String)


public static final String![]()
![]()
pathSeparator
pathSeparatorChar

.
| Constructor Detail |
|---|

public File(String![]()
![]()
pathname)
File instance by converting the given
pathname string into an abstract pathname. If the given string is
the empty string, then the result is the empty abstract pathname.
pathname - A pathname string
NullPointerException

- If the pathname argument is null

public File(String![]()
![]()
parent, String
![]()
![]()
child)
File instance from a parent pathname string
and a child pathname string.
If parent is null then the new
File instance is created as if by invoking the
single-argument File constructor on the given
child pathname string.
Otherwise the parent pathname string is taken to denote
a directory, and the child pathname string is taken to
denote either a directory or a file. If the child pathname
string is absolute then it is converted into a relative pathname in a
system-dependent way. If parent is the empty string then
the new File instance is created by converting
child into an abstract pathname and resolving the result
against a system-dependent default directory. Otherwise each pathname
string is converted into an abstract pathname and the child abstract
pathname is resolved against the parent.
parent - The parent pathname stringchild - The child pathname string
NullPointerException

- If child is null

public File(File![]()
![]()
parent, String
![]()
![]()
child)
File instance from a parent abstract
pathname and a child pathname string.
If parent is null then the new
File instance is created as if by invoking the
single-argument File constructor on the given
child pathname string.
Otherwise the parent abstract pathname is taken to
denote a directory, and the child pathname string is taken
to denote either a directory or a file. If the child
pathname string is absolute then it is converted into a relative
pathname in a system-dependent way. If parent is the empty
abstract pathname then the new File instance is created by
converting child into an abstract pathname and resolving
the result against a system-dependent default directory. Otherwise each
pathname string is converted into an abstract pathname and the child
abstract pathname is resolved against the parent.
parent - The parent abstract pathnamechild - The child pathname string
NullPointerException

- If child is null

public File(URI![]()
![]()
uri)
The exact form of a file: URI is system-dependent, hence the transformation performed by this constructor is also system-dependent.
For a given abstract pathname f it is guaranteed that
new File( f.so long as the original abstract pathname, the URI, and the new abstract pathname are all created in (possibly different invocations of) the same Java virtual machine. This relationship typically does not hold, however, when a file: URI that is created in a virtual machine on one operating system is converted into an abstract pathname in a virtual machine on a different operating system.toURI![]()
![]()
()).equals( f.
getAbsoluteFile![]()
![]()
())
uri - An absolute, hierarchical URI with a scheme equal to
"file", a non-empty path component, and undefined
authority, query, and fragment components
NullPointerException

- If uri is null
IllegalArgumentException

- If the preconditions on the parameter do not holdtoURI()
,
URI

| Method Detail |
|---|

public String![]()
![]()
getName()

public String![]()
![]()
getParent()
null if this pathname does not name a parent directory.
The parent of an abstract pathname consists of the pathname's prefix, if any, and each name in the pathname's name sequence except for the last. If the name sequence is empty then the pathname does not name a parent directory.
null if this pathname
does not name a parent

public File![]()
![]()
getParentFile()
null if this pathname does not name a parent
directory.
The parent of an abstract pathname consists of the pathname's prefix, if any, and each name in the pathname's name sequence except for the last. If the name sequence is empty then the pathname does not name a parent directory.
null if this pathname
does not name a parent

public String![]()
![]()
getPath()
default name-separator character
to
separate the names in the name sequence.

public boolean isAbsolute()
"/". On Microsoft Windows systems, a
pathname is absolute if its prefix is a drive specifier followed by
"\\", or if its prefix is "\\\\".
true if this abstract pathname is absolute,
false otherwise

public String![]()
![]()
getAbsolutePath()
If this abstract pathname is already absolute, then the pathname
string is simply returned as if by the
method. If this abstract pathname is the empty abstract pathname then
the pathname string of the current user directory, which is named by the
system property getPath()

user.dir, is returned. Otherwise this
pathname is resolved in a system-dependent way. On UNIX systems, a
relative pathname is made absolute by resolving it against the current
user directory. On Microsoft Windows systems, a relative pathname is made absolute
by resolving it against the current directory of the drive named by the
pathname, if any; if not, it is resolved against the current user
directory.
SecurityException

- If a required system property value cannot be accessed.isAbsolute()


public File![]()
![]()
getAbsoluteFile()
new File(this.getAbsolutePath()
()).
SecurityException

- If a required system property value cannot be accessed.

public String![]()
![]()
getCanonicalPath() throws IOException
![]()
![]()
A canonical pathname is both absolute and unique. The precise
definition of canonical form is system-dependent. This method first
converts this pathname to absolute form if necessary, as if by invoking the
getAbsolutePath()
method, and then maps it to its unique form in a
system-dependent way. This typically involves removing redundant names
such as "." and ".." from the pathname, resolving
symbolic links (on UNIX platforms), and converting drive letters to a
standard case (on Microsoft Windows platforms).
Every pathname that denotes an existing file or directory has a unique canonical form. Every pathname that denotes a nonexistent file or directory also has a unique canonical form. The canonical form of the pathname of a nonexistent file or directory may be different from the canonical form of the same pathname after the file or directory is created. Similarly, the canonical form of the pathname of an existing file or directory may be different from the canonical form of the same pathname after the file or directory is deleted.
IOException

- If an I/O error occurs, which is possible because the
construction of the canonical pathname may require
filesystem queries
SecurityException

- If a required system property value cannot be accessed, or
if a security manager exists and its SecurityManager.checkRead(java.io.FileDescriptor)

method denies
read access to the file

public File![]()
![]()
getCanonicalFile() throws IOException
![]()
![]()
new File(this.getCanonicalPath()
()).
IOException

- If an I/O error occurs, which is possible because the
construction of the canonical pathname may require
filesystem queries
SecurityException

- If a required system property value cannot be accessed, or
if a security manager exists and its SecurityManager.checkRead(java.io.FileDescriptor)

method denies
read access to the file

public URL