This class consists exclusively of static methods that operate on files, directories, or other types of files.
In most cases, the methods defined here will delegate to the associated file system provider to perform the file operations.
Public Method Summary
static Path | |
static long | |
static long |
copy(InputStream in, Path target, CopyOption... options)
Copies all bytes from an input stream to a file.
|
static Path |
createDirectories(Path dir, FileAttribute...<?> attrs)
Creates a directory by creating all nonexistent parent directories first.
|
static Path | |
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(String prefix, FileAttribute...<?> attrs)
Creates a new directory in the default temporary-file directory, using
the given prefix to generate its name.
|
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 |
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 | |
static boolean | |
static boolean | |
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 | |
static FileTime | |
static UserPrincipal | |
static Set<PosixFilePermission> | |
static boolean | |
static boolean | |
static boolean | |
static boolean | |
static boolean |
isRegularFile(Path path, LinkOption... options)
Tests whether a file is a regular file with opaque content.
|
static boolean | |
static boolean | |
static boolean | |
static Stream<String> | |
static Stream<String> | |
static Stream<Path> | |
static Path | |
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 BufferedReader |
newBufferedReader(Path path)
Opens a file for reading, returning a
BufferedReader to read text
from 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 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 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, String glob)
Opens a directory, returning a
DirectoryStream to iterate over
the entries in the directory. |
static DirectoryStream<Path> |
newDirectoryStream(Path dir, Filter<? super Path> filter)
Opens a directory, returning a
DirectoryStream to iterate over
the entries in the directory. |
static DirectoryStream<Path> |
newDirectoryStream(Path dir)
Opens a directory, returning a
DirectoryStream to iterate over
all 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 | |
static byte[] | |
static List<String> | |
static List<String> | |
static Map<String, Object> |
readAttributes(Path path, String attributes, LinkOption... options)
Reads a set of file attributes as a bulk operation.
|
static <A extends BasicFileAttributes> A |
readAttributes(Path path, Class<A> type, LinkOption... options)
Reads a file's attributes as a bulk operation.
|
static Path | |
static Path |
setAttribute(Path path, String attribute, Object value, LinkOption... options)
Sets the value of a file attribute.
|
static Path | |
static Path | |
static Path | |
static long | |
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 | |
static Path |
walkFileTree(Path start, Set<FileVisitOption> options, int maxDepth, FileVisitor<? super Path> visitor)
Walks a file tree.
|
static Path |
write(Path path, Iterable<? extends CharSequence> lines, OpenOption... options)
Write lines of text 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 |
Inherited Method Summary
Public Methods
public static Path copy (Path source, Path target, CopyOption... options)
Copy a file to a target file.
This method copies a file to the target file with the options
parameter specifying how the copy is performed. By default, the
copy fails if the target file already exists or is a symbolic link,
except if the source and target are the same
file, in
which case the method completes without copying the file. File attributes
are not required to be copied to the target file. If symbolic links are
supported, and the file is a symbolic link, then the final target of the
link is copied. If the file is a directory then it creates an empty
directory in the target location (entries in the directory are not
copied). This method can be used with the walkFileTree
method to copy a directory and all entries in the directory,
or an entire file-tree where required.
The options
parameter may include any of the following:
Option | Description |
---|---|
REPLACE_EXISTING |
If the target file exists, then the target file is replaced if it is not a non-empty directory. If the target file exists and is a symbolic link, then the symbolic link itself, not the target of the link, is replaced. |
COPY_ATTRIBUTES |
Attempts to copy the file attributes associated with this file to
the target file. The exact file attributes that are copied is platform
and file system dependent and therefore unspecified. Minimally, the
last-modified-time is
copied to the target file if supported by both the source and target
file stores. Copying of file timestamps may result in precision
loss. |
NOFOLLOW_LINKS |
Symbolic links are not followed. If the file is a symbolic link,
then the symbolic link itself, not the target of the link, is copied.
It is implementation specific if file attributes can be copied to the
new link. In other words, the COPY_ATTRIBUTES option may be
ignored when copying a symbolic link. |
An implementation of this interface may support additional implementation specific options.
Copying a file is not an atomic operation. If an IOException
is thrown, then it is possible that the target file is incomplete or some
of its file attributes have not been copied from the source file. When
the REPLACE_EXISTING
option is specified and the target file
exists, then the target file is replaced. The check for the existence of
the file and the creation of the new file may not be atomic with respect
to other file system activities.
Usage Example: Suppose we want to copy a file into a directory, giving it the same file name as the source file:
Path source = ... Path newdir = ... Files.copy(source, newdir.resolve(source.getFileName());
Parameters
source | the path to the file to copy |
---|---|
target | the path to the target file (may be associated with a different provider to the source path) |
options | options specifying how the copy should be done |
Returns
- the path to the target file
Throws
UnsupportedOperationException | if the array contains a copy option that is not supported |
---|---|
FileAlreadyExistsException | if the target file exists but cannot be replaced because the
REPLACE_EXISTING option is not specified (optional
specific exception) |
DirectoryNotEmptyException | the REPLACE_EXISTING option is specified but the file
cannot be replaced because it is a non-empty directory
(optional specific exception) |
IOException | if an I/O error occurs |
SecurityException | In the case of the default provider, and a security manager is
installed, the checkRead
method is invoked to check read access to the source file, the
checkWrite is invoked
to check write access to the target file. If a symbolic link is
copied the security manager is invoked to check LinkPermission ("symbolic") .
|
public static long copy (Path source, OutputStream out)
Copies all bytes from a file to an output stream.
If an I/O error occurs reading from the file or writing to the output stream, then it may do so after some bytes have been read or written. Consequently the output stream may be in an inconsistent state. It is strongly recommended that the output stream be promptly closed if an I/O error occurs.
This method may block indefinitely writing to the output stream (or reading from the file). The behavior for the case that the output stream is asynchronously closed or the thread interrupted during the copy is highly output stream and file system provider specific and therefore not specified.
Note that if the given output stream is Flushable
then its flush
method may need to invoked
after this method completes so as to flush any buffered output.
Parameters
source | the path to the file |
---|---|
out | the output stream to write to |
Returns
- the number of bytes read or written
Throws
IOException | if an I/O error occurs when reading or writing |
---|---|
SecurityException | In the case of the default provider, and a security manager is
installed, the checkRead
method is invoked to check read access to the file.
|
public static long copy (InputStream in, Path target, CopyOption... options)
Copies all bytes from an input stream to a file. On return, the input stream will be at end of stream.
By default, the copy fails if the target file already exists or is a
symbolic link. If the REPLACE_EXISTING
option is specified, and the target file already exists,
then it is replaced if it is not a non-empty directory. If the target
file exists and is a symbolic link, then the symbolic link is replaced.
In this release, the REPLACE_EXISTING
option is the only option
required to be supported by this method. Additional options may be
supported in future releases.
If an I/O error occurs reading from the input stream or writing to the file, then it may do so after the target file has been created and after some bytes have been read or written. Consequently the input stream may not be at end of stream and may be in an inconsistent state. It is strongly recommended that the input stream be promptly closed if an I/O error occurs.
This method may block indefinitely reading from the input stream (or writing to the file). The behavior for the case that the input stream is asynchronously closed or the thread interrupted during the copy is highly input stream and file system provider specific and therefore not specified.
Usage example: Suppose we want to capture a web page and save it to a file:
Path path = ... URI u = URI.create("http://java.sun.com/"); try (InputStream in = u.toURL().openStream()) { Files.copy(in, path); }
Parameters
in | the input stream to read from |
---|---|
target | the path to the file |
options | options specifying how the copy should be done |
Returns
- the number of bytes read or written
Throws
IOException | if an I/O error occurs when reading or writing |
---|---|
FileAlreadyExistsException | if the target file exists but cannot be replaced because the
REPLACE_EXISTING option is not specified (optional
specific exception) |
DirectoryNotEmptyException | the REPLACE_EXISTING option is specified but the file
cannot be replaced because it is a non-empty directory
(optional specific exception) * |
UnsupportedOperationException | if options contains a copy option that is not supported |
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. Where the
REPLACE_EXISTING option is specified, the security
manager's checkDelete
method is invoked to check that an existing file can be deleted.
|
public static Path createDirectories (Path dir, FileAttribute...<?> attrs)
Creates a directory by creating all nonexistent parent directories first.
Unlike the createDirectory
method, an exception
is not thrown if the directory could not be created because it already
exists.
The attrs
parameter is optional file-attributes
to set atomically when creating the nonexistent
directories. Each file 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.
If this method fails, then it may do so after creating some, but not all, of the parent directories.
Parameters
dir | the directory to create |
---|---|
attrs | an optional list of file attributes to set atomically when creating the directory |
Returns
- the directory
Throws
UnsupportedOperationException | if the array contains an attribute that cannot be set atomically when creating the directory |
---|---|
FileAlreadyExistsException | if dir exists but is not a directory (optional specific
exception) |
IOException | if an I/O error occurs |
SecurityException | in the case of the default provider, and a security manager is
installed, the checkWrite
method is invoked prior to attempting to create a directory and
its checkRead is
invoked for each parent directory that is checked. If dir is not an absolute path then its toAbsolutePath may need to be invoked to get its absolute path.
This may invoke the security manager's checkPropertyAccess
method to check access to the system property user.dir
|
public static Path createDirectory (Path dir, FileAttribute...<?> attrs)
Creates a new directory. The check for the existence of the file and the
creation of the directory if it does not exist are a single operation
that is atomic with respect to all other filesystem activities that might
affect the directory. The createDirectories
method should be used where it is required to create all nonexistent
parent directories first.
The attrs
parameter is optional file-attributes
to set atomically when creating the directory. 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.
Parameters
dir | the directory to create |
---|---|
attrs | an optional list of file attributes to set atomically when creating the directory |
Returns
- the directory
Throws
UnsupportedOperationException | if the array contains an attribute that cannot be set atomically when creating the directory |
---|---|
FileAlreadyExistsException | if a directory could not otherwise be created because a file of that name already exists (optional specific exception) |
IOException | if an I/O error occurs or the parent directory 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 new directory.
|
public static Path createFile (Path path, FileAttribute...<?> attrs)
Creates a new and empty file, failing if the file already exists. The check for the existence of the file and the creation of the new file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the directory.
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.
Parameters
path | the path to the file to create |
---|---|
attrs | an optional list of file attributes to set atomically when creating the file |
Returns
- the file
Throws
UnsupportedOperationException | if the array contains an attribute that cannot be set atomically when creating the file |
---|---|
FileAlreadyExistsException | if a file of that name already exists (optional specific exception) |
IOException | if an I/O error occurs or the parent directory 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 new file.
|
public static Path createLink (Path link, Path existing)
Creates a new link (directory entry) for an existing file (optional operation).
The link
parameter locates the directory entry to create.
The existing
parameter is the path to an existing file. This
method creates a new directory entry for the file so that it can be
accessed using link
as the path. On some file systems this is
known as creating a "hard link". Whether the file attributes are
maintained for the file or for each directory entry is file system
specific and therefore not specified. Typically, a file system requires
that all links (directory entries) for a file be on the same file system.
Furthermore, on some platforms, the Java virtual machine may require to
be started with implementation specific privileges to create hard links
or to create links to directories.
Parameters
link | the link (directory entry) to create |
---|---|
existing | a path to an existing file |
Returns
- the path to the link (directory entry)
Throws
UnsupportedOperationException | if the implementation does not support adding an existing file to a directory |
---|---|
FileAlreadyExistsException | if the entry could not otherwise be created because a file of that name already exists (optional specific exception) |
IOException | if an I/O error occurs |
SecurityException | In the case of the default provider, and a security manager
is installed, it denies LinkPermission ("hard")
or its checkWrite
method denies write access to either the link or the
existing file.
|
public static Path createSymbolicLink (Path link, Path target, FileAttribute...<?> attrs)
Creates a symbolic link to a target (optional operation).
The target
parameter is the target of the link. It may be an
absolute
or relative path and may not exist. When
the target is a relative path then file system operations on the resulting
link are relative to the path of the link.
The attrs
parameter is optional attributes
to set atomically when creating the link. 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.
Where symbolic links are supported, but the underlying FileStore
does not support symbolic links, then this may fail with an IOException
. Additionally, some operating systems may require that the
Java virtual machine be started with implementation specific privileges to
create symbolic links, in which case this method may throw IOException
.
Parameters
link | the path of the symbolic link to create |
---|---|
target | the target of the symbolic link |
attrs | the array of attributes to set atomically when creating the symbolic link |
Returns
- the path to the symbolic link
Throws
UnsupportedOperationException | if the implementation does not support symbolic links or the array contains an attribute that cannot be set atomically when creating the symbolic link |
---|---|
FileAlreadyExistsException | if a file with the name already exists (optional specific exception) |
IOException | if an I/O error occurs |
SecurityException | In the case of the default provider, and a security manager
is installed, it denies LinkPermission ("symbolic")
or its checkWrite
method denies write access to the path of the symbolic link.
|
public 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. The resulting Path
is
associated with the default FileSystem
.
This method works in exactly the manner specified by createTempDirectory(Path, String, FileAttribute[])
method for the case
that the dir
parameter is the temporary-file directory.
Parameters
prefix | the prefix string to be used in generating the directory's name;
may be null |
---|---|
attrs | an optional list of file attributes to set atomically when creating the directory |
Returns
- the path to the newly created directory that did not exist before this method was invoked
Throws
IllegalArgumentException | if the prefix cannot be used to generate a candidate directory 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 the temporary-file directory 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 when creating the
directory.
|
public 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. The resulting Path
is associated
with the same FileSystem
as the given directory.
The details as to how the name of the directory is constructed is
implementation dependent and therefore not specified. Where possible
the prefix
is used to construct candidate names.
As with the createTempFile
methods, this method is only
part of a temporary-file facility. A shutdown-hook
, or the File.deleteOnExit()
mechanism may be
used to delete the directory automatically.
The attrs
parameter is optional file-attributes
to set atomically when creating the directory. 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.
Parameters
dir | the path to directory in which to create the directory |
---|---|
prefix | the prefix string to be used in generating the directory's name;
may be null |
attrs | an optional list of file attributes to set atomically when creating the directory |
Returns
- the path to the newly created directory that did not exist before this method was invoked
Throws
IllegalArgumentException | if the prefix cannot be used to generate a candidate directory 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 when creating the
directory.
|
public 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. 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.
|
public 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. The resulting Path
is associated with the default FileSystem
.
This method works in exactly the manner specified by the
createTempFile(Path, String, String, FileAttribute[])
method for
the case that the dir
parameter is the temporary-file directory.
Parameters
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 the temporary-file directory 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.
|
public static void delete (Path path)
Deletes a file.
An implementation may require to examine the file to determine if the file is a directory. Consequently this method may not be atomic with respect to other file system operations. If the file is a symbolic link then the symbolic link itself, not the final target of the link, is deleted.
If the file is a directory then the directory must be empty. In some
implementations a directory has entries for special files or links that
are created when the directory is created. In such implementations a
directory is considered empty when only the special entries exist.
This method can be used with the walkFileTree
method to delete a directory and all entries in the directory, or an
entire file-tree where required.
On some operating systems it may not be possible to remove a file when it is open and in use by this Java virtual machine or other programs.
Parameters
path | the path to the file to delete |
---|
Throws
NoSuchFileException | if the file does not exist (optional specific exception) |
---|---|
DirectoryNotEmptyException | if the file is a directory and could not otherwise be deleted because the directory is not empty (optional specific exception) |
IOException | if an I/O error occurs |
SecurityException | In the case of the default provider, and a security manager is
installed, the SecurityManager.checkDelete(String) method
is invoked to check delete access to the file
|
public static boolean deleteIfExists (Path path)
Deletes a file if it exists.
As with the delete(Path)
method, an
implementation may need to examine the file to determine if the file is a
directory. Consequently this method may not be atomic with respect to
other file system operations. If the file is a symbolic link, then the
symbolic link itself, not the final target of the link, is deleted.
If the file is a directory then the directory must be empty. In some implementations a directory has entries for special files or links that are created when the directory is created. In such implementations a directory is considered empty when only the special entries exist.
On some operating systems it may not be possible to remove a file when it is open and in use by this Java virtual machine or other programs.
Parameters
path | the path to the file to delete |
---|
Returns
true
if the file was deleted by this method;false
if the file could not be deleted because it did not exist
Throws
DirectoryNotEmptyException | if the file is a directory and could not otherwise be deleted because the directory is not empty (optional specific exception) |
---|---|
IOException | if an I/O error occurs |
SecurityException | In the case of the default provider, and a security manager is
installed, the SecurityManager.checkDelete(String) method
is invoked to check delete access to the file.
|
public static boolean exists (Path path, LinkOption... options)
Tests whether a file exists.
The options
parameter 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. If the option NOFOLLOW_LINKS
is present then symbolic links are not followed.
Note that the result of this method is immediately outdated. If this method indicates the file exists then there is no guarantee that a subsequence access will succeed. Care should be taken when using this method in security sensitive applications.
Parameters
path | the path to the file to test |
---|---|
options | options indicating how symbolic links are handled . |
Returns
true
if the file exists;false
if the file does not exist or its existence cannot be determined.
Throws
SecurityException | In the case of the default provider, the SecurityManager.checkRead(String) is invoked to check
read access to the file. |
---|
See Also
public 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.
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 DirectoryStream
s.
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 |
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. |
See Also
public static Object getAttribute (Path path, String attribute, LinkOption... options)
Reads the value of a file attribute.
The attribute
parameter identifies the attribute to be read
and takes the form:
[view-name:]attribute-namewhere 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.
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.
Usage Example:
Suppose we require the user ID of the file owner on a system that
supports a "unix
" view:
Path path = ... int uid = (Integer)Files.getAttribute(path, "unix:uid");
Parameters
path | the path to the file |
---|---|
attribute | the attribute to read |
options | options indicating how symbolic links are handled |
Returns
- the attribute value
Throws
UnsupportedOperationException | if the attribute view is not available |
---|---|
IllegalArgumentException | if the attribute name is not specified or is not recognized |
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 invoked to check for additional permissions.
|
public static V getFileAttributeView (Path path, Class<V> type, LinkOption... options)
Returns a file attribute view of a given type.
A file attribute view provides a read-only or updatable view of a
set of file attributes. This method is intended to be used where the file
attribute view defines type-safe methods to read or update the file
attributes. The type
parameter is the type of the attribute view
required and the method returns an instance of that type if supported.
The BasicFileAttributeView
type supports access to the basic
attributes of a file. Invoking this method to select a file attribute
view of that type will always return an instance of that class.
The options
array may be used to indicate how symbolic links
are handled by the resulting file attribute view for the case that the
file is a symbolic link. By default, symbolic links are followed. If the
option NOFOLLOW_LINKS
is present then
symbolic links are not followed. This option is ignored by implementations
that do not support symbolic links.
Usage Example: Suppose we want read or set a file's ACL, if supported:
Path path = ... AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class); if (view != null) { List<AclEntry> acl = view.getAcl(); : }
Parameters
path | the path to the file |
---|---|
type | the Class object corresponding to the file attribute view |
options | options indicating how symbolic links are handled |
Returns
- a file attribute view of the specified type, or
null
if the attribute view type is not available