GeezerVison is showing docs.oracle.com — Files (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
- Summary:
- Nested |
- Field |
- Constr |
- Method
- Detail:
- Field |
- Constr |
- Method
compact1, compact2, compact3
java.nio.file
Class Files
-
-
Method Summary
All Methods Static Methods Concrete Methods
| Modifier and Type |
Method |
Description |
static long |
copy(InputStream in,
Path target,
CopyOption... options) |
Copies all bytes from an input stream to a file.
|
static long |
copy(Path source,
OutputStream out) |
Copies all bytes from a file to an output stream.
|
static Path |
copy(Path source,
Path target,
CopyOption... options) |
Copy a file to a target file.
|
static Path |
createDirectories(Path dir,
FileAttribute<?>... attrs) |
Creates a directory by creating all nonexistent parent directories first.
|
static Path |
createDirectory(Path dir,
FileAttribute<?>... attrs) |
Creates a new directory.
|
static Path |
createFile(Path path,
FileAttribute<?>... attrs) |
Creates a new and empty file, failing if the file already exists.
|
static Path |
createLink(Path link,
Path existing) |
Creates a new link (directory entry) for an existing file (optional
operation).
|
static Path |
createSymbolicLink(Path link,
Path target,
FileAttribute<?>... attrs) |
Creates a symbolic link to a target (optional operation).
|
static Path |
createTempDirectory(Path dir,
String prefix,
FileAttribute<?>... attrs) |
Creates a new directory in the specified directory, using the given
prefix to generate its name.
|
static Path |
createTempDirectory(String prefix,
FileAttribute<?>... attrs) |
Creates a new directory in the default temporary-file directory, using
the given prefix to generate its name.
|
static Path |
createTempFile(Path dir,
String prefix,
String suffix,
FileAttribute<?>... attrs) |
Creates a new empty file in the specified directory, using the given
prefix and suffix strings to generate its name.
|
static Path |
createTempFile(String prefix,
String suffix,
FileAttribute<?>... attrs) |
Creates an empty file in the default temporary-file directory, using
the given prefix and suffix to generate its name.
|
static void |
delete(Path path) |
Deletes a file.
|
static boolean |
deleteIfExists(Path path) |
Deletes a file if it exists.
|
static boolean |
exists(Path path,
LinkOption... options) |
Tests whether a file exists.
|
static Stream<Path> |
find(Path start,
int maxDepth,
BiPredicate<Path,BasicFileAttributes> matcher,
FileVisitOption... options) |
Return a Stream that is lazily populated with Path by searching for files in a file tree rooted at a given starting
file.
|
static Object |
getAttribute(Path path,
String attribute,
LinkOption... options) |
Reads the value of a file attribute.
|
static <V extends FileAttributeView> V |
getFileAttributeView(Path path,
Class<V> type,
LinkOption... options) |
Returns a file attribute view of a given type.
|
static FileStore |
getFileStore(Path path) |
Returns the FileStore representing the file store where a file
is located.
|
static FileTime |
getLastModifiedTime(Path path,
LinkOption... options) |
Returns a file's last modified time.
|
static UserPrincipal |
getOwner(Path path,
LinkOption... options) |
Returns the owner of a file.
|
static Set<PosixFilePermission> |
getPosixFilePermissions(Path path,
LinkOption... options) |
Returns a file's POSIX file permissions.
|
static boolean |
isDirectory(Path path,
LinkOption... options) |
Tests whether a file is a directory.
|
static boolean |
isExecutable(Path path) |
Tests whether a file is executable.
|
static boolean |
isHidden(Path path) |
Tells whether or not a file is considered hidden.
|
static boolean |
isReadable(Path path) |
Tests whether a file is readable.
|
static boolean |
isRegularFile(Path path,
LinkOption... options) |
Tests whether a file is a regular file with opaque content.
|
static boolean |
isSameFile(Path path,
Path path2) |
Tests if two paths locate the same file.
|
static boolean |
isSymbolicLink(Path path) |
Tests whether a file is a symbolic link.
|
static boolean |
isWritable(Path path) |
Tests whether a file is writable.
|
static Stream<String> |
lines(Path path) |
Read all lines from a file as a Stream.
|
static Stream<String> |
lines(Path path,
Charset cs) |
Read all lines from a file as a Stream.
|
static Stream<Path> |
list(Path dir) |
Return a lazily populated Stream, the elements of
which are the entries in the directory.
|
static Path |
move(Path source,
Path target,
CopyOption... options) |
Move or rename a file to a target file.
|
static BufferedReader |
newBufferedReader(Path path) |
Opens a file for reading, returning a BufferedReader to read text
from the file in an efficient manner.
|
static BufferedReader |
newBufferedReader(Path path,
Charset cs) |
Opens a file for reading, returning a BufferedReader that may be
used to read text from the file in an efficient manner.
|
static BufferedWriter |
newBufferedWriter(Path path,
Charset cs,
OpenOption... options) |
Opens or creates a file for writing, returning a BufferedWriter
that may be used to write text to the file in an efficient manner.
|
static BufferedWriter |
newBufferedWriter(Path path,
OpenOption... options) |
Opens or creates a file for writing, returning a BufferedWriter
to write text to the file in an efficient manner.
|
static SeekableByteChannel |
newByteChannel(Path path,
OpenOption... options) |
Opens or creates a file, returning a seekable byte channel to access the
file.
|
static SeekableByteChannel |
newByteChannel(Path path,
Set<? extends OpenOption> options,
FileAttribute<?>... attrs) |
Opens or creates a file, returning a seekable byte channel to access the
file.
|
static DirectoryStream<Path> |
newDirectoryStream(Path dir) |
Opens a directory, returning a DirectoryStream to iterate over
all entries in the directory.
|
static DirectoryStream<Path> |
newDirectoryStream(Path dir,
DirectoryStream.Filter<? super Path> filter) |
Opens a directory, returning a DirectoryStream to iterate over
the entries in the directory.
|
static DirectoryStream<Path> |
newDirectoryStream(Path dir,
String glob) |
Opens a directory, returning a DirectoryStream to iterate over
the entries in the directory.
|
static InputStream |
newInputStream(Path path,
OpenOption... options) |
Opens a file, returning an input stream to read from the file.
|
static OutputStream |
newOutputStream(Path path,
OpenOption... options) |
Opens or creates a file, returning an output stream that may be used to
write bytes to the file.
|
static boolean |
notExists(Path path,
LinkOption... options) |
Tests whether the file located by this path does not exist.
|
static String |
probeContentType(Path path) |
Probes the content type of a file.
|
static byte[] |
readAllBytes(Path path) |
Reads all the bytes from a file.
|
static List<String> |
readAllLines(Path path) |
Read all lines from a file.
|
static List<String> |
readAllLines(Path path,
Charset cs) |
Read all lines from a file.
|
static <A extends BasicFileAttributes> A |
readAttributes(Path path,
Class<A> type,
LinkOption... options) |
Reads a file's attributes as a bulk operation.
|
static Map<String,Object> |
readAttributes(Path path,
String attributes,
LinkOption... options) |
Reads a set of file attributes as a bulk operation.
|
static Path |
readSymbolicLink(Path link) |
Reads the target of a symbolic link (optional operation).
|
static Path |
setAttribute(Path path,
String attribute,
Object value,
LinkOption... options) |
Sets the value of a file attribute.
|
static Path |
setLastModifiedTime(Path path,
FileTime time) |
Updates a file's last modified time attribute.
|
static Path |
setOwner(Path path,
UserPrincipal owner) |
Updates the file owner.
|
static Path |
setPosixFilePermissions(Path path,
Set<PosixFilePermission> perms) |
Sets a file's POSIX permissions.
|
static long |
size(Path path) |
Returns the size of a file (in bytes).
|
static Stream<Path> |
walk(Path start,
FileVisitOption... options) |
Return a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file.
|
static Stream<Path> |
walk(Path start,
int maxDepth,
FileVisitOption... options) |
Return a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file.
|
static Path |
walkFileTree(Path start,
FileVisitor<? super Path> visitor) |
Walks a file tree.
|
static Path |
walkFileTree(Path start,
Set<FileVisitOption> options,
int maxDepth,
FileVisitor<? super Path> visitor) |
Walks a file tree.
|
static Path |
write(Path path,
byte[] bytes,
OpenOption... options) |
Writes bytes to a file.
|
static Path |
write(Path path,
Iterable<? extends CharSequence> lines,
Charset cs,
OpenOption... options) |
Write lines of text to a file.
|
static Path |
write(Path path,
Iterable<? extends CharSequence> lines,
OpenOption... options) |
Write lines of text to a file.
|
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
createTempFile
public static Path createTempFile(Path dir,
String prefix,
String suffix,
FileAttribute<?>... attrs)
throws IOException
Creates a new empty file in the specified directory, using the given
prefix and suffix strings to generate its name. The resulting
Path is associated with the same FileSystem as the given
directory.
The details as to how the name of the file is constructed is
implementation dependent and therefore not specified. Where possible
the prefix and suffix are used to construct candidate
names in the same manner as the File.createTempFile(String,String,File) method.
As with the File.createTempFile methods, this method is only
part of a temporary-file facility. Where used as a work files,
the resulting file may be opened using the DELETE_ON_CLOSE option so that the
file is deleted when the appropriate close method is invoked.
Alternatively, a shutdown-hook, or the
File.deleteOnExit() mechanism may be used to delete the
file automatically.
The attrs parameter is optional file-attributes to set atomically when creating the file. Each attribute
is identified by its name. If more than one
attribute of the same name is included in the array then all but the last
occurrence is ignored. When no file attributes are specified, then the
resulting file may have more restrictive access permissions to files
created by the File.createTempFile(String,String,File)
method.
- Parameters:
dir - the path to directory in which to create the file
prefix - the prefix string to be used in generating the file's name;
may be null
suffix - the suffix string to be used in generating the file's name;
may be null, in which case ".tmp" is used
attrs - an optional list of file attributes to set atomically when
creating the file
- Returns:
- the path to the newly created file that did not exist before
this method was invoked
- Throws:
IllegalArgumentException - if the prefix or suffix parameters cannot be used to generate
a candidate file name
UnsupportedOperationException - if the array contains an attribute that cannot be set atomically
when creating the directory
IOException - if an I/O error occurs or dir does not exist
SecurityException - In the case of the default provider, and a security manager is
installed, the checkWrite
method is invoked to check write access to the file.
-
setAttribute
public static Path setAttribute(Path path,
String attribute,
Object value,
LinkOption... options)
throws IOException
Sets the value of a file attribute.
The attribute parameter identifies the attribute to be set
and takes the form:
[view-name:]attribute-name
where square brackets [...] delineate an optional component and the
character ':' stands for itself.
view-name is the name of a FileAttributeView that identifies a set of file attributes. If not
specified then it defaults to "basic", the name of the file
attribute view that identifies the basic set of file attributes common to
many file systems. attribute-name is the name of the attribute
within the set.
The options array may be used to indicate how symbolic links
are handled for the case that the file is a symbolic link. By default,
symbolic links are followed and the file attribute of the final target
of the link is set. If the option NOFOLLOW_LINKS is present then symbolic links are not followed.
Usage Example:
Suppose we want to set the DOS "hidden" attribute:
Path path = ...
Files.setAttribute(path, "dos:hidden", true);
- Parameters:
path - the path to the file
attribute - the attribute to set
value - the attribute value
options - options indicating how symbolic links are handled
- Returns:
- the
path parameter
- Throws:
UnsupportedOperationException - if the attribute view is not available
IllegalArgumentException - if the attribute name is not specified, or is not recognized, or
the attribute value is of the correct type but has an
inappropriate value
ClassCastException - if the attribute value is not of the expected type or is a
collection containing elements that are not of the expected
type
IOException - if an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is
installed, its checkWrite
method denies write access to the file. If this method is invoked
to set security sensitive attributes then the security manager
may be invoked to check for additional permissions.
-
readAttributes
public static Map<String,Object> readAttributes(Path path,
String attributes,
LinkOption... options)
throws IOException
Reads a set of file attributes as a bulk operation.
The attributes parameter identifies the attributes to be read
and takes the form:
[view-name:]attribute-list
where square brackets [...] delineate an optional component and the
character ':' stands for itself.
view-name is the name of a FileAttributeView that identifies a set of file attributes. If not
specified then it defaults to "basic", the name of the file
attribute view that identifies the basic set of file attributes common to
many file systems.
The attribute-list component is a comma separated list of
zero or more names of attributes to read. If the list contains the value
"*" then all attributes are read. Attributes that are not supported
are ignored and will not be present in the returned map. It is
implementation specific if all attributes are read as an atomic operation
with respect to other file system operations.
The following examples demonstrate possible values for the attributes parameter:
"*" |
Read all basic-file-attributes. |
"size,lastModifiedTime,lastAccessTime" |
Reads the file size, last modified time, and last access time
attributes. |
"posix:*" |
Read all POSIX-file-attributes. |
"posix:permissions,owner,size" |
Reads the POSX file permissions, owner, and file size. |
The options array may be used to indicate how symbolic links
are handled for the case that the file is a symbolic link. By default,
symbolic links are followed and the file attribute of the final target
of the link is read. If the option NOFOLLOW_LINKS is present then symbolic links are not followed.
- Parameters:
path - the path to the file
attributes - the attributes to read
options - options indicating how symbolic links are handled
- Returns:
- a map of the attributes returned; The map's keys are the
attribute names, its values are the attribute values
- Throws:
UnsupportedOperationException - if the attribute view is not available
IllegalArgumentException - if no attributes are specified or an unrecognized attributes is
specified
IOException - if an I/O error occurs
SecurityException - In the case of the default provider, and a security manager is
installed, its checkRead
method denies read access to the file. If this method is invoked
to read security sensitive attributes then the security manager
may be invoke to check for additional permissions.
-
walkFileTree
public static Path walkFileTree(Path start,
Set<FileVisitOption> options,
int maxDepth,
FileVisitor<? super Path> visitor)
throws IOException
Walks a file tree.
This method walks a file tree rooted at a given starting file. The
file tree traversal is depth-first with the given FileVisitor invoked for each file encountered. File tree traversal
completes when all accessible files in the tree have been visited, or a
visit method returns a result of TERMINATE. Where a visit method terminates due an IOException,
an uncaught error, or runtime exception, then the traversal is terminated
and the error or exception is propagated to the caller of this method.
For each file encountered this method attempts to read its BasicFileAttributes. If the file is not a
directory then the visitFile method is
invoked with the file attributes. If the file attributes cannot be read,
due to an I/O exception, then the visitFileFailed method is invoked with the I/O exception.
Where the file is a directory, and the directory could not be opened,
then the visitFileFailed method is invoked with the I/O exception,
after which, the file tree walk continues, by default, at the next
sibling of the directory.
Where the directory is opened successfully, then the entries in the
directory, and their descendants are visited. When all entries
have been visited, or an I/O error occurs during iteration of the
directory, then the directory is closed and the visitor's postVisitDirectory method is invoked.
The file tree walk then continues, by default, at the next sibling
of the directory.
By default, symbolic links are not automatically followed by this
method. If the options parameter contains the FOLLOW_LINKS option then symbolic links are
followed. When following links, and the attributes of the target cannot
be read, then this method attempts to get the BasicFileAttributes
of the link. If they can be read then the visitFile method is
invoked with the attributes of the link (otherwise the visitFileFailed
method is invoked as specified above).
If the options parameter contains the FOLLOW_LINKS option then this method keeps
track of directories visited so that cycles can be detected. A cycle
arises when there is an entry in a directory that is an ancestor of the
directory. Cycle detection is done by recording the file-key of directories,
or if file keys are not available, by invoking the isSameFile method to test if a directory is the same file as an
ancestor. When a cycle is detected it is treated as an I/O error, and the
visitFileFailed method is invoked with
an instance of FileSystemLoopException.
The maxDepth parameter is the maximum number of levels of
directories to visit. A value of 0 means that only the starting
file is visited, unless denied by the security manager. A value of
MAX_VALUE may be used to indicate that all
levels should be visited. The visitFile method is invoked for all
files, including directories, encountered at maxDepth, unless the
basic file attributes cannot be read, in which case the visitFileFailed method is invoked.
If a visitor returns a result of null then NullPointerException is thrown.
When a security manager is installed and it denies access to a file
(or directory), then it is ignored and the visitor is not invoked for
that file (or directory).
- Parameters:
start - the starting file
options - options to configure the traversal
maxDepth - the maximum number of directory levels to visit
visitor - the file visitor to invoke for each file
- Returns:
- the starting file
- Throws:
IllegalArgumentException - if the maxDepth parameter is negative
SecurityException - If the security manager denies access to the starting file.
In the case of the default provider, the checkRead method is invoked
to check read access to the directory.
IOException - if an I/O error is thrown by a visitor method
-
write
public static Path write(Path path,
Iterable<? extends CharSequence> lines,
Charset cs,
OpenOption... options)
throws IOException
Write lines of text to a file. Each line is a char sequence and is
written to the file in sequence with each line terminated by the
platform's line separator, as defined by the system property line.separator. Characters are encoded into bytes using the specified
charset.
The options parameter specifies how the the file is created
or opened. If no options are present then this method works as if the
CREATE, TRUNCATE_EXISTING, and WRITE options are present. In other words, it
opens the file for writing, creating the file if it doesn't exist, or
initially truncating an existing regular-file to
a size of 0. The method ensures that the file is closed when all
lines have been written (or an I/O error or other runtime exception is
thrown). If an I/O error occurs then it may do so after the file has
created or truncated, or after some bytes have been written to the file.
- Parameters:
path - the path to the file
lines - an object to iterate over the char sequences
cs - the charset to use for encoding
options - options specifying how the file is opened
- Returns:
- the path
- Throws:
IOException - if an I/O error occurs writing to or creating the file, or the
text cannot be encoded using the specified charset
UnsupportedOperationException - if an unsupported option is specified
SecurityException - In the case of the default provider, and a security manager is
installed, the checkWrite
method is invoked to check write access to the file.
-
find
public static Stream<Path> find(Path start,
int maxDepth,
BiPredicate<Path,BasicFileAttributes> matcher,
FileVisitOption... options)
throws IOException
Return a Stream that is lazily populated with Path by searching for files in a file tree rooted at a given starting
file.
This method walks the file tree in exactly the manner specified by
the walk method. For each file encountered, the given
BiPredicate is invoked with its Path and BasicFileAttributes. The Path object is obtained as if by
resolving the relative path against start and is only included in the returned Stream if
the BiPredicate returns true. Compare to calling filter on the Stream
returned by walk method, this method may be more efficient by
avoiding redundant retrieval of the BasicFileAttributes.
The returned stream encapsulates one or more DirectoryStreams.
If timely disposal of file system resources is required, the
try-with-resources construct should be used to ensure that the
stream's close method is invoked after the stream
operations are completed. Operating on a closed stream will result in an
IllegalStateException.
If an IOException is thrown when accessing the directory
after returned from this method, it is wrapped in an UncheckedIOException which will be thrown from the method that caused
the access to take place.
- Parameters:
start - the starting file
maxDepth - the maximum number of directory levels to search
matcher - the function used to decide whether a file should be included
in the returned stream
options - options to configure the traversal
- Returns:
- the
Stream of Path
- Throws:
IllegalArgumentException - if the maxDepth parameter is negative
SecurityException - If the security manager denies access to the starting file.
In the case of the default provider, the checkRead method is invoked
to check read access to the directory.
IOException - if an I/O error is thrown when accessing the starting file.
- Since:
- 1.8
- See Also:
walk(Path, int, FileVisitOption...)
Java™ Platform Standard Ed. 8
- Summary:
- Nested |
- Field |
- Constr |
- Method
- Detail:
- Field |
- Constr |
- Method
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.
|