Modifier and Type | Method and Description |
---|---|
static void |
addPermission(Path path,
PosixFilePermission permission)
Adds a permission to a file or directory.
|
static void |
addPermissionRecursive(Path path,
PosixFilePermission permission)
Recursively adds a permission to a directory.
|
static void |
copyLarge(Path source,
Path target)
Copy a file to a target file in an efficient way.
|
static void |
copyRecursive(Path source,
Path target)
Copy a path to a target path recursively.
|
static Path |
defaultDirectory(FileSystem fs)
Returns the default directory for the given file system.
|
static void |
deleteRecursive(Path path)
Deletes a path recursively.
|
static boolean |
deleteRecursiveIfExists(Path path)
Deletes a path recursively if it exists.
|
static DirectoryStream.Filter<Path> |
directoryFilter()
Returns a directory filter that matches directories.
|
static boolean |
isEmpty(Path path)
Test if the given path is empty.
|
static boolean |
isLocal(Path path)
Determines if the given path is associated with the default (local) file
system.
|
static List<Path> |
listDirectory(Path dir)
Gets a list of the files and directories in the given directory.
|
static List<Path> |
listDirectoryFiles(Path dir)
Gets a list of the files in the given directory.
|
static List<Path> |
listDirectoryFilesRecursive(Path dir)
Gets a list of the files in the given directory and all sub-directories.
|
static List<Path> |
listDirectoryRecursive(Path dir)
Gets a list of the files and directories in the given directory and all
sub-directories.
|
static String |
readAllString(Path path,
Charset cs)
Reads the contents of a file as a string.
|
static DirectoryStream.Filter<Path> |
regularFileFilter()
Returns a directory filter that matches regular files.
|
static void |
removePermission(Path path,
PosixFilePermission permission)
Removes a permission from a file or directory.
|
static void |
removePermissionRecursive(Path path,
PosixFilePermission permission)
Recursively removes a permission from a directory.
|
static void |
setExecutable(Path path)
Adds the
OWNER_EXECUTE permission to a file or directory. |
static void |
setPermissionRecursive(Path path,
Set<PosixFilePermission> permissions)
Recursively sets permissions for a directory.
|
static Path |
write(Path path,
String string,
Charset cs,
OpenOption... options)
Writes a string to a file.
|
public static DirectoryStream.Filter<Path> regularFileFilter()
public static DirectoryStream.Filter<Path> directoryFilter()
public static void deleteRecursive(Path path) throws IOException
If the path is a file or an empty directory, this method is equivalent to
Files.delete(Path)
. If the path is a non-empty directory, the
directory, all sub-directories, and all child files are deleted.
This operation is not atomic with respect to other file system operations. As a result, the path may be left in a partially-deleted state if this method fails.
path
- the path to deleteNoSuchFileException
- if the path does not existIOException
- if an I/O error occurs while deleting the pathpublic static boolean deleteRecursiveIfExists(Path path) throws IOException
If the path is a file or an empty directory, this method is equivalent to
Files.deleteIfExists(Path)
. If the path is a non-empty directory,
the directory, all sub-directories, and all child files are deleted.
This operation is not atomic with respect to other file system operations. As a result, the path may be left in a partially-deleted state if this method fails.
path
- the path to deletetrue
if the path was deleted by this method,
false
if the path did not existIOException
- if an I/O error occurs while deleting the pathpublic static void copyLarge(Path source, Path target) throws IOException
This method is equivalent to
Files.copy(source, target, StandardCopyOptions.REPLACE_EXISTING)
, but
may be significantly faster depending on the file systems of the source
and the target.
source
- the file to copytarget
- the target fileIllegalArgumentException
- if source
is not a regular file
or target
is a directory.IOException
- if an IO error occurs while copyingpublic static void copyRecursive(Path source, Path target) throws IOException
If the source is a file or empty directory, this method is equivalent to
Files.copy(source, target)
. If the source is a non-empty directory, the
directory and all descendants are copied to the target path. The
destination must not exist.
If an IOException
occurs while copying files and directories,
this method may leave a partial copy at the target path. Users may catch
these exceptions and recursively delete the target path if this is a
problem.
source
- the path to copytarget
- the target pathFileAlreadyExistsException
- if the target already existsNoSuchFileException
- if any parents of the target do not existIOException
- if an I/O error occurs while copyingpublic static String readAllString(Path path, Charset cs) throws IOException
path
- the file to readcs
- the charset to use for decodingIOException
- if an I/O error occurs while reading a fileFiles.readAllBytes(Path)
public static Path write(Path path, String string, Charset cs, OpenOption... options) throws IOException
path
- the path to the filestring
- the string to writecs
- the charset to use for encodingoptions
- options specifying how the file is openedIOException
- if an I/O error occurs while writing the fileFiles.write(Path, Iterable, Charset, OpenOption...)
public static Path defaultDirectory(FileSystem fs)
public static void addPermission(Path path, PosixFilePermission permission) throws IOException
path
- the path to the file or directorypermission
- the permission to addIOException
- if an I/O error occurs while adding the permissionpublic static void addPermissionRecursive(Path path, PosixFilePermission permission) throws IOException
path
- the path to the directorypermission
- the permission to addIOException
- if an I/O error occurs while adding the permissionpublic static void setExecutable(Path path) throws IOException
OWNER_EXECUTE
permission to a file or directory.path
- the path to the file or directoryIOException
- if an I/O error occurs while setting the permissionpublic static void removePermission(Path path, PosixFilePermission permission) throws IOException
path
- the path to the file or directorypermission
- the permission to removeIOException
- if an I/O error occurs while removing the permissionpublic static void removePermissionRecursive(Path path, PosixFilePermission permission) throws IOException
path
- the path to the directorypermission
- the permission to removeIOException
- if an I/O error occurs while removing the permissionpublic static void setPermissionRecursive(Path path, Set<PosixFilePermission> permissions) throws IOException
path
- the path to the directorypermissions
- the desired POSIX file permissionsIOException
- if an I/O error occurs while setting the permissionpublic static boolean isEmpty(Path path) throws IOException
path
- the pathtrue
if the path is emptyIOException
- if an I/O error occurs while accessing the pathpublic static List<Path> listDirectory(Path dir) throws IOException
dir
- the Path
to listIOException
- if an I/O error occurs while listing entriespublic static List<Path> listDirectoryFiles(Path dir) throws IOException
dir
- the Path
to listIOException
- if an I/O error occurs while listing entriespublic static List<Path> listDirectoryRecursive(Path dir) throws IOException
dir
- the Path
to listIOException
- if an I/O error occurs while descending the file treepublic static List<Path> listDirectoryFilesRecursive(Path dir) throws IOException
dir
- the Path
to listIOException
- if an I/O error occurs while descending the file treepublic static boolean isLocal(Path path)