GeezerVison is showing docs.oracle.com — File (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
compact1, compact2, compact3
java.io
Class File
-
-
Field Summary
Fields
| Modifier and Type |
Field |
Description |
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
Constructors
| Constructor |
Description |
File(File parent,
String child) |
Creates a new File instance from a parent abstract
pathname and a child pathname string.
|
File(String pathname) |
Creates a new File instance by converting the given
pathname string into an abstract pathname.
|
File(String parent,
String child) |
Creates a new File instance from a parent pathname string
and a child pathname string.
|
File(URI uri) |
Creates a new File instance by converting the given
file: URI into an abstract pathname.
|
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods
| Modifier and Type |
Method |
Description |
boolean |
canExecute() |
Tests whether the application can execute the file denoted by this
abstract pathname.
|
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(File pathname) |
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(String prefix,
String suffix) |
Creates an empty file in the default temporary-file directory, using
the given prefix and suffix to generate its name.
|
static File |
createTempFile(String prefix,
String suffix,
File directory) |
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(Object obj) |
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.
|
long |
getFreeSpace() |
Returns the number of unallocated bytes in the partition named by this abstract path name.
|
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.
|
long |
getTotalSpace() |
Returns the size of the partition named by this
abstract pathname.
|
long |
getUsableSpace() |
Returns the number of bytes available to this virtual machine on the
partition named by this abstract pathname.
|
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(FilenameFilter filter) |
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(FileFilter filter) |
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(FilenameFilter filter) |
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(File dest) |
Renames the file denoted by this abstract pathname.
|
boolean |
setExecutable(boolean executable) |
A convenience method to set the owner's execute permission for this
abstract pathname.
|
boolean |
setExecutable(boolean executable,
boolean ownerOnly) |
Sets the owner's or everybody's execute permission for this abstract
pathname.
|
boolean |
setLastModified(long time) |
Sets the last-modified time of the file or directory named by this
abstract pathname.
|
boolean |
setReadable(boolean readable) |
A convenience method to set the owner's read permission for this abstract
pathname.
|
boolean |
setReadable(boolean readable,
boolean ownerOnly) |
Sets the owner's or everybody's read permission for this abstract
pathname.
|
boolean |
setReadOnly() |
Marks the file or directory named by this abstract pathname so that
only read operations are allowed.
|
boolean |
setWritable(boolean writable) |
A convenience method to set the owner's write permission for this abstract
pathname.
|
boolean |
setWritable(boolean writable,
boolean ownerOnly) |
Sets the owner's or everybody's write permission for this abstract
pathname.
|
Path |
toPath() |
|
String |
toString() |
Returns the pathname string of this abstract pathname.
|
URI |
toURI() |
Constructs a file: URI that represents this abstract pathname.
|
URL |
toURL() |
Deprecated.
This method does not automatically escape characters that
are illegal in URLs. It is recommended that new code convert an
abstract pathname into a URL by first converting it into a URI, via the
toURI method, and then converting the URI into a URL
via the URI.toURL method.
|
-
-
Field Detail
-
separatorChar
public static final char separatorChar
The system-dependent default name-separator character. This field is
initialized to contain the first character of the value of the system
property file.separator. On UNIX systems the value of this
field is '/'; on Microsoft Windows systems it is '\\'.
- See Also:
System.getProperty(java.lang.String)
-
pathSeparatorChar
public static final char pathSeparatorChar
The system-dependent path-separator character. This field is
initialized to contain the first character of the value of the system
property 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 ';'.
- See Also:
System.getProperty(java.lang.String)
-
Constructor Detail
-
File
public File(String pathname)
Creates a new 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.
- Parameters:
pathname - A pathname string
- Throws:
NullPointerException - If the pathname argument is null
-
File
public File(URI uri)
Creates a new File instance by converting the given
file: URI into an abstract pathname.
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.toURI()).equals( f.getAbsoluteFile())
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.
- Parameters:
uri - An absolute, hierarchical URI with a scheme equal to
"file", a non-empty path component, and undefined
authority, query, and fragment components
- Throws:
NullPointerException - If uri is null
IllegalArgumentException - If the preconditions on the parameter do not hold
- Since:
- 1.4
- See Also:
toURI(),
URI
-
Method Detail
-
getPath
public String getPath()
Converts this abstract pathname into a pathname string. The resulting
string uses the default name-separator character to
separate the names in the name sequence.
- Returns:
- The string form of this abstract pathname
-
isAbsolute
public boolean isAbsolute()
Tests whether this abstract pathname is absolute. The definition of
absolute pathname is system dependent. On UNIX systems, a pathname is
absolute if its prefix is "/". On Microsoft Windows systems, a
pathname is absolute if its prefix is a drive specifier followed by
"\\", or if its prefix is "\\\\".
- Returns:
true if this abstract pathname is absolute,
false otherwise
-
toURL
@Deprecated
public URL toURL()
throws MalformedURLException
Deprecated. This method does not automatically escape characters that
are illegal in URLs. It is recommended that new code convert an
abstract pathname into a URL by first converting it into a URI, via the
toURI method, and then converting the URI into a URL
via the URI.toURL method.
Converts this abstract pathname into a file: URL. The
exact form of the URL is system-dependent. If it can be determined that
the file denoted by this abstract pathname is a directory, then the
resulting URL will end with a slash.
- Returns:
- A URL object representing the equivalent file URL
- Throws:
MalformedURLException - If the path cannot be parsed as a URL
- Since:
- 1.2
- See Also:
toURI(),
URI,
URI.toURL(),
URL
-
toURI
public URI toURI()
Constructs a file: URI that represents this abstract pathname.
The exact form of the URI is system-dependent. If it can be
determined that the file denoted by this abstract pathname is a
directory, then the resulting URI will end with a slash.
For a given abstract pathname f, it is guaranteed that
new File( f.toURI()).equals( f.getAbsoluteFile())
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. Due to the system-dependent nature of abstract
pathnames, however, this relationship typically does not hold 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.
Note that when this abstract pathname represents a UNC pathname then
all components of the UNC (including the server name component) are encoded
in the URI path. The authority component is undefined, meaning
that it is represented as null. The Path class defines the
toUri method to encode the server name in the authority
component of the resulting URI. The toPath method
may be used to obtain a Path representing this abstract pathname.
- Returns:
- An absolute, hierarchical URI with a scheme equal to
"file", a path representing this abstract pathname,
and undefined authority, query, and fragment components
- Throws:
SecurityException - If a required system property value cannot
be accessed.
- Since:
- 1.4
- See Also:
File(java.net.URI),
URI,
URI.toURL()
-
canWrite
public boolean canWrite()
Tests whether the application can modify the file denoted by this
abstract pathname. On some platforms it may be possible to start the
Java virtual machine with special privileges that allow it to modify
files that are marked read-only. Consequently this method may return
true even though the file is marked read-only.
- Returns:
true if and only if the file system actually
contains a file denoted by this abstract pathname and
the application is allowed to write to the file;
false otherwise.
- Throws:
SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String)
method denies write access to the file
-
isFile
public boolean isFile()
Tests whether the file denoted by this abstract pathname is a normal
file. A file is normal if it is not a directory and, in
addition, satisfies other system-dependent criteria. Any non-directory
file created by a Java application is guaranteed to be a normal file.
Where it is required to distinguish an I/O exception from the case
that the file is not a normal file, or where several attributes of the
same file are required at the same time, then the Files.readAttributes method may be used.
- Returns:
true if and only if the file denoted by this
abstract pathname exists and is a normal file;
false otherwise
- Throws:
SecurityException - If a security manager exists and its SecurityManager.checkRead(java.lang.String)
method denies read access to the file
-
isHidden
public boolean isHidden()
Tests whether the file named by this abstract pathname is a hidden
file. The exact definition of hidden is system-dependent. On
UNIX systems, a file is considered to be hidden if its name begins with
a period character ('.'). On Microsoft Windows systems, a file is
considered to be hidden if it has been marked as such in the filesystem.
- Returns:
true if and only if the file denoted by this
abstract pathname is hidden according to the conventions of the
underlying platform
- Throws:
SecurityException - If a security manager exists and its SecurityManager.checkRead(java.lang.String)
method denies read access to the file
- Since:
- 1.2
-
length
public long length()
Returns the length of the file denoted by this abstract pathname.
The return value is unspecified if this pathname denotes a directory.
Where it is required to distinguish an I/O exception from the case
that 0L is returned, or where several attributes of the same file
are required at the same time, then the Files.readAttributes method may be used.
- Returns:
- The length, in bytes, of the file denoted by this abstract
pathname, or
0L if the file does not exist. Some
operating systems may return 0L for pathnames
denoting system-dependent entities such as devices or pipes.
- Throws:
SecurityException - If a security manager exists and its SecurityManager.checkRead(java.lang.String)
method denies read access to the file
-
delete
public boolean delete()
Deletes the file or directory denoted by this abstract pathname. If
this pathname denotes a directory, then the directory must be empty in
order to be deleted.
Note that the Files class defines the delete method to throw an IOException
when a file cannot be deleted. This is useful for error reporting and to
diagnose why a file cannot be deleted.
- Returns:
true if and only if the file or directory is
successfully deleted; false otherwise
- Throws:
SecurityException - If a security manager exists and its SecurityManager.checkDelete(java.lang.String) method denies
delete access to the file
-
list
public String[] list()
Returns an array of strings naming the files and directories in the
directory denoted by this abstract pathname.
If this abstract pathname does not denote a directory, then this
method returns null. Otherwise an array of strings is
returned, one for each file or directory in the directory. Names
denoting the directory itself and the directory's parent directory are
not included in the result. Each string is a file name rather than a
complete path.
There is no guarantee that the name strings in the resulting array
will appear in any specific order; they are not, in particular,
guaranteed to appear in alphabetical order.
Note that the Files class defines the newDirectoryStream method to
open a directory and iterate over the names of the files in the directory.
This may use less resources when working with very large directories, and
may be more responsive when working with remote directories.
- Returns:
- An array of strings naming the files and directories in the
directory denoted by this abstract pathname. The array will be
empty if the directory is empty. Returns
null if
this abstract pathname does not denote a directory, or if an
I/O error occurs.
- Throws:
SecurityException - If a security manager exists and its SecurityManager.checkRead(String) method denies read access to
the directory
-
setReadOnly
public boolean setReadOnly()
Marks the file or directory named by this abstract pathname so that
only read operations are allowed. After invoking this method the file
or directory will not change until it is either deleted or marked
to allow write access. On some platforms it may be possible to start the
Java virtual machine with special privileges that allow it to modify
files that are marked read-only. Whether or not a read-only file or
directory may be deleted depends upon the underlying system.
- Returns:
true if and only if the operation succeeded;
false otherwise
- Throws:
SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String)
method denies write access to the named file
- Since:
- 1.2
-
canExecute
public boolean canExecute()
Tests whether the application can execute the file denoted by this
abstract pathname. On some platforms it may be possible to start the
Java virtual machine with special privileges that allow it to execute
files that are not marked executable. Consequently this method may return
true even though the file does not have execute permissions.
- Returns:
true if and only if the abstract pathname exists
and the application is allowed to execute the file
- Throws:
SecurityException - If a security manager exists and its SecurityManager.checkExec(java.lang.String)
method denies execute access to the file
- Since:
- 1.6
-
compareTo
public int compareTo(File pathname)
Compares two abstract pathnames lexicographically. The ordering
defined by this method depends upon the underlying system. On UNIX
systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows
systems it is not.
- Specified by:
compareTo in interface Comparable<File>
- Parameters:
pathname - The abstract pathname to be compared to this abstract
pathname
- 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
- Since:
- 1.2
-
equals
public boolean equals(Object obj)
Tests this abstract pathname for equality with the given object.
Returns true if and only if the argument is not
null and is an abstract pathname that denotes the same file
or directory as this abstract pathname. Whether or not two abstract
pathnames are equal depends upon the underlying system. On UNIX
systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows
systems it is not.
- Overrides:
equals in class Object
- Parameters:
obj - The object to be compared with this abstract pathname
- Returns:
true if and only if the objects are the same;
false otherwise
- See Also:
Object.hashCode(),
HashMap
-
hashCode
public int hashCode()
Computes a hash code for this abstract pathname. Because equality of
abstract pathnames is inherently system-dependent, so is the computation
of their hash codes. On UNIX systems, the hash code of an abstract
pathname is equal to the exclusive or of the hash code
of its pathname string and the decimal value
1234321. On Microsoft Windows systems, the hash
code is equal to the exclusive or of the hash code of
its pathname string converted to lower case and the decimal
value 1234321. Locale is not taken into account on
lowercasing the pathname string.
- Overrides:
hashCode in class Object
- Returns:
- A hash code for this abstract pathname
- See Also:
Object.equals(java.lang.Object),
System.identityHashCode(java.lang.Object)
-
toString
public String toString()
Returns the pathname string of this abstract pathname. This is just the
string returned by the getPath() method.
- Overrides:
toString in class Object
- Returns:
- The string form of this abstract pathname
-
toPath
public Path toPath()
Returns a java.nio.file.Path object constructed from the
this abstract path. The resulting Path is associated with the
default-filesystem.
The first invocation of this method works as if invoking it were
equivalent to evaluating the expression:
FileSystems.getDefault().getPath(this.getPath());
Subsequent invocations of this method return the same Path.
If this abstract pathname is the empty abstract pathname then this
method returns a Path that may be used to access the current
user directory.
- Returns:
- a
Path constructed from this abstract path
- Throws:
InvalidPathException - if a Path object cannot be constructed from the abstract
path (see FileSystem.getPath)
- Since:
- 1.7
- See Also:
Path.toFile()
Java™ Platform Standard Ed. 8
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright © 1993, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|