Class MoreFiles
-
- com.google.common.io.MoreFiles
-
@Beta @GwtIncompatible public final class MoreFiles extends
Static utilities for use with instances, intended to complement .Many methods provided by Guava's
Files
class for instances are now available via the JDK's class forPath
- check the JDK's class if a sibling method fromFiles
appears to be missing from this class.- Since:
- 21.0
- Author:
- Colin Decker
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ByteSink
path, ... options)
Returns a view of the givenpath
as aByteSink
.static ByteSource
path, ... options)
Returns a view of the givenpath
as aByteSource
.static CharSink
path, charset, ... options)
static CharSource
path, charset, ... options)
static void
path, <?>... attrs)
Creates any necessary but nonexistent parent directories of the specified path.static void
path, RecursiveDeleteOption... options)
Deletes all files within the directory at the givenpath
recursively.static void
path, RecursiveDeleteOption... options)
Deletes the file or directory at the givenpath
recursively.static boolean
path1, path2)
Returns true if the files located by the given paths exist, are not directories, and contain the same bytes.static >
fileTraverser()
Returns aTraverser
instance for the file and directory tree.static
path)
Returns the for the file at the given path, or the empty string if the file has no extension.static
path)
Returns the file name without its or path.static >
... options)
Returns a predicate that returns the result of on input paths with the given link options.static >
... options)
Returns a predicate that returns the result of on input paths with the given link options.static >
dir)
Returns an immutable list of paths to the files contained in the given directory.static void
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.
-
-
-
Method Detail
-
asByteSource
public static path, ... options)
Returns a view of the givenpath
as aByteSource
.Any provided are used when opening streams to the file and may affect the behavior of the returned source and the streams it provides. See for the standard options that may be provided. Providing no options is equivalent to providing the option.
-
asByteSink
public static path, ... options)
Returns a view of the givenpath
as aByteSink
.Any provided are used when opening streams to the file and may affect the behavior of the returned sink and the streams it provides. See for the standard options that may be provided. Providing no options is equivalent to providing the , and options.
-
asCharSource
public static path, charset, ... options)
Returns a view of the givenpath
as aCharSource
using the givencharset
.Any provided are used when opening streams to the file and may affect the behavior of the returned source and the streams it provides. See for the standard options that may be provided. Providing no options is equivalent to providing the option.
-
asCharSink
public static path, charset, ... options)
Returns a view of the givenpath
as aCharSink
using the givencharset
.Any provided are used when opening streams to the file and may affect the behavior of the returned sink and the streams it provides. See for the standard options that may be provided. Providing no options is equivalent to providing the , and options.
-
listFiles
public static > dir) throws
Returns an immutable list of paths to the files contained in the given directory.- Throws:
- if the file does not exist (optional specific exception)
- if the file could not be opened because it is not a directory (optional specific exception)
- if an I/O error occurs
-
fileTraverser
public static > fileTraverser()
Returns aTraverser
instance for the file and directory tree. The returned traverser starts from a and will return all files and directories it encounters.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.
If the passed to one of the traversal methods does not exist or is not a directory, no exception will be thrown and the returned will contain a single element: that path.
may be thrown when iterating instances created by this traverser if an is thrown by a call to
listFiles(Path)
.Example:
MoreFiles.fileTraverser().depthFirstPreOrder(Paths.get("/"))
may return the following paths:["/", "/etc", "/etc/config.txt", "/etc/fonts", "/home", "/home/alice", ...]
- Since:
- 23.5
-
isDirectory
public static > ... options)
Returns a predicate that returns the result of on input paths with the given link options.
-
isRegularFile
public static > ... options)
Returns a predicate that returns the result of on input paths with the given link options.
-
equal
public static boolean path1, path2) throws
Returns true if the files located by the given paths exist, are not directories, and contain the same bytes.- Throws:
- if an I/O error occurs
- Since:
- 22.0
-
touch
public static void path) throws
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.- Throws:
-
createParentDirectories
public static void path, <?>... attrs) throws
Creates any necessary but nonexistent parent directories of the specified path. Note that if this operation fails, it may have succeeded in creating some (but not all) of the necessary parent directories. The parent directory is created with the givenattrs
.- Throws:
- if an I/O error occurs, or if any necessary but nonexistent parent directories of the specified file could not be created.
-
getFileExtension
public static path)
Returns the for the file at the given path, or the empty string if the file has no extension. The result does not include the '.
'.Note: This method simply returns everything after the last '
.
' in the file's name as determined by . It does not account for any filesystem-specific behavior that the 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 .
-
getNameWithoutExtension
public static path)
Returns the file name without its or path. This is similar to thebasename
unix command. The result does not include the '.
'.
-
deleteRecursively
public static void path, RecursiveDeleteOption... options) throws
Deletes the file or directory at the givenpath
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 .Warning: Security of recursive deletes
On a file system that supports symbolic links and does not support , 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, passRecursiveDeleteOption.ALLOW_INSECURE
to this method to override that behavior.- Throws:
- if
path
does not exist (optional specific exception)InsecureRecursiveDeleteException
- if the security of recursive deletes can't be guaranteed for the file system andRecursiveDeleteOption.ALLOW_INSECURE
was not specified- if
path
or any file in the subtree rooted at it can't be deleted for any reason
-
deleteDirectoryContents
public static void path, RecursiveDeleteOption... options) throws
Deletes all files within the directory at the givenpath
recursively. Does not delete the directory itself. Deletes symbolic links, not their targets (subject to the caveat below). Ifpath
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 .Warning: Security of recursive deletes
On a file system that supports symbolic links and does not support , 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, passRecursiveDeleteOption.ALLOW_INSECURE
to this method to override that behavior.- Throws:
- if
path
does not exist (optional specific exception)- 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 andRecursiveDeleteOption.ALLOW_INSECURE
was not specified- if one or more files can't be deleted for any reason
-
-