public class SafeFiles
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
canonicalFileName(java.nio.file.Path path)
Path.getFileName() includes a slash sometimes for some reason. |
static java.nio.file.attribute.FileAttribute<?>[] |
getOwnerOnlyFileAttributes(AttributeTarget attributeTarget)
Get a set of file attributes for file creation with owner-only access, if possible.
|
static java.util.stream.Stream<java.nio.file.Path> |
noLeakFileList(java.nio.file.Path dir)
A version of
Files.list(Path) that won't leak resources. |
static void |
tryHardToDelete(java.nio.file.Path path)
Tries to delete a path.
|
static void |
tryHardToDeleteDir(java.nio.file.Path directory)
Recursively uses
tryHardToDelete(Path) to cleanup directories before deleting them. |
public static java.util.stream.Stream<java.nio.file.Path> noLeakFileList(java.nio.file.Path dir) throws java.io.IOException
Files.list(Path)
that won't leak resources.
Instead, it immediately consumes the entire listing into a List
and
calls Collection.stream()
.
dir
- the directory to listjava.io.IOException
- if an I/O error occurspublic static java.lang.String canonicalFileName(java.nio.file.Path path)
Path.getFileName()
includes a slash sometimes for some reason.
This will get rid of it.path
- the path to get the file name forpublic static void tryHardToDeleteDir(java.nio.file.Path directory) throws java.io.IOException
tryHardToDelete(Path)
to cleanup directories before deleting them.directory
- the directory to deletejava.io.IOException
- if an error occurs trying to delete the directorypublic static void tryHardToDelete(java.nio.file.Path path) throws java.io.IOException
IOException
with both errors.path
- the path to deletejava.io.IOException
- if the path could not be deleted after multiple attemptspublic static java.nio.file.attribute.FileAttribute<?>[] getOwnerOnlyFileAttributes(AttributeTarget attributeTarget)
On POSIX, this returns o+rw (and o+x if directory), on Windows it returns nothing.