@Beta @GwtIncompatible public final class MoreFiles extends Object
Path
instances, intended to complement Files
.
Many methods provided by Guava's Files
class for File
instances are
now available via the JDK's Files
class for Path
- check the JDK's
class if a sibling method from Files
appears to be missing from this class.
Modifier and Type | Method and Description |
---|---|
static ByteSink |
asByteSink(Path path,
OpenOption... options)
Returns a view of the given
path as a ByteSink . |
static ByteSource |
asByteSource(Path path,
OpenOption... options)
Returns a view of the given
path as a ByteSource . |
static CharSink |
asCharSink(Path path,
Charset charset,
OpenOption... options)
|
static CharSource |
asCharSource(Path path,
Charset charset,
OpenOption... options)
|
static void |
createParentDirectories(Path path,
FileAttribute<?>... attrs)
Creates any necessary but nonexistent parent directories of the specified path.
|
static void |
deleteDirectoryContents(Path path,
RecursiveDeleteOption... options)
Deletes all files within the directory at the given
path
recursively. |
static void |
deleteRecursively(Path path,
RecursiveDeleteOption... options)
Deletes the file or directory at the given
path recursively. |
static TreeTraverser<Path> |
directoryTreeTraverser()
Returns a
TreeTraverser for traversing a directory tree. |
static boolean |
equal(Path path1,
Path path2)
Returns true if the files located by the given paths exist, are not directories, and contain
the same bytes.
|
static String |
getFileExtension(Path path)
Returns the file extension for
the file at the given path, or the empty string if the file has no extension.
|
static String |
getNameWithoutExtension(Path path)
Returns the file name without its
file extension or path.
|
static Predicate<Path> |
isDirectory(LinkOption... options)
Returns a predicate that returns the result of
Files.isDirectory(Path,
LinkOption...) on input paths with the given link options. |
static Predicate<Path> |
isRegularFile(LinkOption... options)
Returns a predicate that returns the result of
Files.isRegularFile(Path,
LinkOption...) on input paths with the given link options. |
static ImmutableList<Path> |
listFiles(Path dir)
Returns an immutable list of paths to the files contained in the given directory.
|
static void |
touch(Path path)
Like the unix command of the same name, creates an empty file or updates the last modified
timestamp of the existing file at the given path to the current system time.
|
public static ByteSource asByteSource(Path path, OpenOption... options)
path
as a ByteSource
.
Any open options provided are used when opening streams to the file
and may affect the behavior of the returned source and the streams it provides. See StandardOpenOption
for the standard options that may be provided. Providing no options is
equivalent to providing the READ
option.
public static ByteSink asByteSink(Path path, OpenOption... options)
path
as a ByteSink
.
Any open options provided are used when opening streams to the file
and may affect the behavior of the returned sink and the streams it provides. See StandardOpenOption
for the standard options that may be provided. Providing no options is
equivalent to providing the CREATE
, TRUNCATE_EXISTING
and WRITE
options.
public static CharSource asCharSource(Path path, Charset charset, OpenOption... options)
path
as a CharSource
using the given charset
.
Any open options provided are used when opening streams to the file
and may affect the behavior of the returned source and the streams it provides. See StandardOpenOption
for the standard options that may be provided. Providing no options is
equivalent to providing the READ
option.
public static CharSink asCharSink(Path path, Charset charset, OpenOption... options)
path
as a CharSink
using the given charset
.
Any open options provided are used when opening streams to the file
and may affect the behavior of the returned sink and the streams it provides. See StandardOpenOption
for the standard options that may be provided. Providing no options is
equivalent to providing the CREATE
, TRUNCATE_EXISTING
and WRITE
options.
public static ImmutableList<Path> listFiles(Path dir) throws IOException
NoSuchFileException
- if the file does not exist (optional specific exception)NotDirectoryException
- if the file could not be opened because it is not a directory
(optional specific exception)IOException
- if an I/O error occurspublic static TreeTraverser<Path> directoryTreeTraverser()
TreeTraverser
for traversing a directory tree. The returned traverser
attempts to avoid following symbolic links to directories. However, the traverser cannot
guarantee that it will not follow symbolic links to directories as it is possible for a
directory to be replaced with a symbolic link between checking if the file is a directory and
actually reading the contents of that directory.
Note that if the Path
passed to one of the traversal methods does not exist, no
exception will be thrown and the returned Iterable
will contain a single element: that
path.
DirectoryIteratorException
may be thrown when iterating Iterable
instances
created by this traverser if an IOException
is thrown by a call to
listFiles(Path)
.
public static Predicate<Path> isDirectory(LinkOption... options)
Files.isDirectory(Path,
LinkOption...)
on input paths with the given link options.public static Predicate<Path> isRegularFile(LinkOption... options)
Files.isRegularFile(Path,
LinkOption...)
on input paths with the given link options.public static boolean equal(Path path1, Path path2) throws IOException
IOException
- if an I/O error occurspublic static void touch(Path path) throws IOException
IOException
public static void createParentDirectories(Path path, FileAttribute<?>... attrs) throws IOException
attrs
.IOException
- if an I/O error occurs, or if any necessary but nonexistent parent
directories of the specified file could not be created.public static String getFileExtension(Path path)
.
'.
Note: This method simply returns everything after the last '.
' in the file's
name as determined by Path.getFileName()
. It does not account for any filesystem-specific
behavior that the Path
API does not already account for. For example, on NTFS it will
report "txt"
as the extension for the filename "foo.exe:.txt"
even though NTFS
will drop the ":.txt"
part of the name when the file is actually created on the
filesystem due to NTFS's Alternate Data Streams.
public static String getNameWithoutExtension(Path path)
basename
unix command. The result does not include the '.
'.public static void deleteRecursively(Path path, RecursiveDeleteOption... options) throws IOException
path
recursively. Deletes symbolic links,
not their targets (subject to the caveat below).
If an I/O exception occurs attempting to read, open or delete any file under the given
directory, this method skips that file and continues. All such exceptions are collected and,
after attempting to delete all files, an IOException
is thrown containing those
exceptions as suppressed exceptions.
On a file system that supports symbolic links and does not support
SecureDirectoryStream
, it is possible for a recursive delete to delete files and
directories that are outside the directory being deleted. This can happen if, after
checking that a file is a directory (and not a symbolic link), that directory is replaced by a
symbolic link to an outside directory before the call that opens the directory to read its
entries.
By default, this method throws InsecureRecursiveDeleteException
if it can't
guarantee the security of recursive deletes. If you wish to allow the recursive deletes
anyway, pass RecursiveDeleteOption.ALLOW_INSECURE
to this method to override that
behavior.
NoSuchFileException
- if path
does not exist (optional specific
exception)InsecureRecursiveDeleteException
- if the security of recursive deletes can't be
guaranteed for the file system and RecursiveDeleteOption.ALLOW_INSECURE
was not
specifiedIOException
- if path
or any file in the subtree rooted at it can't be deleted
for any reasonpublic static void deleteDirectoryContents(Path path, RecursiveDeleteOption... options) throws IOException
path
recursively. Does not delete the directory itself. Deletes
symbolic links, not their targets (subject to the caveat below). If path
itself is
a symbolic link to a directory, that link is followed and the contents of the directory it
targets are deleted.
If an I/O exception occurs attempting to read, open or delete any file under the given
directory, this method skips that file and continues. All such exceptions are collected and,
after attempting to delete all files, an IOException
is thrown containing those
exceptions as suppressed exceptions.
On a file system that supports symbolic links and does not support
SecureDirectoryStream
, it is possible for a recursive delete to delete files and
directories that are outside the directory being deleted. This can happen if, after
checking that a file is a directory (and not a symbolic link), that directory is replaced by a
symbolic link to an outside directory before the call that opens the directory to read its
entries.
By default, this method throws InsecureRecursiveDeleteException
if it can't
guarantee the security of recursive deletes. If you wish to allow the recursive deletes
anyway, pass RecursiveDeleteOption.ALLOW_INSECURE
to this method to override that
behavior.
NoSuchFileException
- if path
does not exist (optional specific
exception)NotDirectoryException
- if the file at path
is not a directory (optional
specific exception)InsecureRecursiveDeleteException
- if the security of recursive deletes can't be
guaranteed for the file system and RecursiveDeleteOption.ALLOW_INSECURE
was not
specifiedIOException
- if one or more files can't be deleted for any reasonCopyright © 2010–2017. All rights reserved.