Class FileUtil
java.lang.Object
eu.cloudnetservice.utils.base.io.FileUtil
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final DirectoryStream.Filter<Path> private static final FileSystemProviderprivate static final org.slf4j.Loggerstatic final Path -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcopy(@Nullable InputStream inputStream, @Nullable OutputStream outputStream) Copies the input stream to the output stream.static voidcopy(@Nullable InputStream inputStream, @Nullable Path target) Copies the input stream to given target path.static voidCopies the source file to the given destination.static voidcopyDirectory(@NonNull Path from, @NonNull Path to) Copies the target directory to the given destination and creates all needed parent directories.static voidcopyDirectory(@NonNull Path from, @NonNull Path to, DirectoryStream.Filter<Path> filter) Copies the target directory to the given destination and creates all needed parent directories.static voidcreateDirectory(@Nullable Path directoryPath) Creates all needed parent directories and the given directory only if the given path does not exist already.Resolves a random path in the temp directory of the cloud and creates all needed parents directories for a temporary file including theTEMP_DIR.static voidDeletes the file or directory at the given path.static voidensureChild(@NonNull Path root, @NonNull Path child) Ensures that the given child path is a child path of the given root path and throwing an exception otherwise.static <T> @UnknownNullability TmapZipFile(@NonNull Path zip, @NonNull CheckedFunction1<FileSystem, T> mapper, T def) Opens the given zip into aFileSystemand passes it into the given function.static voidMoves the file at the source path to the given destination path.static voidopenZipFile(@NonNull Path zip, @NonNull CheckedConsumer<FileSystem> consumer) Opens the given zip into aFileSystemand passes it into the given consumer.Resolves the given array for the base path and returns the final path after all children are resolved one after another.static voidwalkFileTree(@NonNull Path root, @NonNull BiConsumer<Path, Path> consumer) Walks the file tree, while visiting directories too, starting at the given path and passes the root directory together with the next file or directory into the bi consumer.static voidwalkFileTree(@NonNull Path root, @NonNull BiConsumer<Path, Path> consumer, boolean visitDirs) Walks the file tree, but only visits directories if specified, starting at the given path and passes the root directory together with the next file or directory into the bi consumer.static voidwalkFileTree(@NonNull Path root, @NonNull BiConsumer<Path, Path> consumer, boolean visitDirectories, @NonNull String glob) Walks the file tree while filtering for the given glob and only visiting directories if specified.static voidwalkFileTree(@NonNull Path root, @NonNull BiConsumer<Path, Path> consumer, boolean visitDirectories, DirectoryStream.Filter<Path> filter) Walks the file tree while filtering with the given filter and only visiting directories if specified.
-
Field Details
-
TEMP_DIR
-
LOGGER
private static final org.slf4j.Logger LOGGER -
ACCEPTING_FILTER
-
JAR_FILE_SYSTEM_PROVIDER
-
ZIP_FILE_SYSTEM_PROPERTIES
-
-
Constructor Details
-
FileUtil
private FileUtil()
-
-
Method Details
-
openZipFile
public static void openZipFile(@NonNull @NonNull Path zip, @NonNull @NonNull CheckedConsumer<FileSystem> consumer) Opens the given zip into aFileSystemand passes it into the given consumer. The file system is closed automatically after all operations are done.- Parameters:
zip- the path to open as zip file system.consumer- the consumer accepting the file system.- Throws:
NullPointerException- if the given zip path or consumer is null.
-
mapZipFile
public static <T> @UnknownNullability T mapZipFile(@NonNull @NonNull Path zip, @NonNull @NonNull CheckedFunction1<FileSystem, T> mapper, @Nullable T def) Opens the given zip into aFileSystemand passes it into the given function. The file system is closed automatically after all operations are done.- Type Parameters:
T- the return type of the mapping function.- Parameters:
zip- the path to open as zip file system.mapper- the mapper to apply to the given zip file.def- the return value to return from the method when an exception occurs.- Returns:
- the mapped value based on the file system, or def if an exception occurs.
- Throws:
NullPointerException- if the given zip path or consumer is null.
-
move
public static void move(@NonNull @NonNull Path from, @NonNull @NonNull Path to, CopyOption @NonNull ... options) Moves the file at the source path to the given destination path. Use the copy options to specify the behavior when moving. This method is equivalent withFiles.move(from, to, options...)but catches the thrown exceptions and just prints them into the error log.- Parameters:
from- the file to move.to- the destination path.options- options that specify the moving behavior.- Throws:
NullPointerException- if the given paths are null or null values for the options are passed.
-
copy
public static void copy(@Nullable @Nullable InputStream inputStream, @Nullable @Nullable OutputStream outputStream) Copies the input stream to the output stream. This method is equivalent withinputStream.transferTo(outputStream)but catches the thrown exceptions and just prints them into the error log.- Parameters:
inputStream- the input stream to copy from.outputStream- the target output stream.
-
copy
public static void copy(@Nullable @Nullable InputStream inputStream, @Nullable @Nullable Path target) Copies the input stream to given target path. This method creates all needed parent directories first and then usesFileUtil.copy(inputStream, out)to copy to the destination, by converting the path into a new output stream.- Parameters:
inputStream- the input stream to copy from.target- the destination path.
-
copy
Copies the source file to the given destination. This method creates all needed parent directories first and then usesFiles.copy(from, to, StandardCopyOption.REPLACE_EXISTING)to copy the file, therefore existing files are replaced. All thrown I/O exceptions are caught and redirected into the error log.- Parameters:
from- the source path.to- the destination path.- Throws:
NullPointerException- if any of the given paths is null.
-
copyDirectory
Copies the target directory to the given destination and creates all needed parent directories. It walks the whole file tree and copies every file and subdirectory without any filtering. This method is equivalent toFileUtil.copyDirectory(from, to, null).- Parameters:
from- the source path.to- the destination path.- Throws:
NullPointerException- if any of the given paths is null.
-
copyDirectory
public static void copyDirectory(@NonNull @NonNull Path from, @NonNull @NonNull Path to, @Nullable DirectoryStream.Filter<Path> filter) Copies the target directory to the given destination and creates all needed parent directories. It walks the whole file tree and copies every file and subdirectory. If the given filter is null no filtering is done, otherwise the filter is applied while walking the file tree.- Parameters:
from- the source path.to- the destination path.filter- the filter to use while walking the file tree.- Throws:
NullPointerException- if any of the given paths is null.
-
delete
Deletes the file or directory at the given path. If the path points to a file it is deleted usingFiles.delete(path)occurring exceptions are ignored here, otherwise if the path points to a directory this walks the file tree and deletes the files and directories recursively.- Parameters:
path- the target to delete.
-
createTempFile
-
walkFileTree
public static void walkFileTree(@NonNull @NonNull Path root, @NonNull @NonNull BiConsumer<Path, Path> consumer) Walks the file tree, while visiting directories too, starting at the given path and passes the root directory together with the next file or directory into the bi consumer. This method is equivalent toFileUtil.walkFileTree(root, consumer, true).- Parameters:
root- the root path to start at.consumer- the consumer accepting all files and directories.- Throws:
NullPointerException- if the given path or consumer is null.
-
walkFileTree
public static void walkFileTree(@NonNull @NonNull Path root, @NonNull @NonNull BiConsumer<Path, Path> consumer, boolean visitDirs) Walks the file tree, but only visits directories if specified, starting at the given path and passes the root directory together with the next file or directory into the bi consumer. This method is equivalent toFileUtil.walkFileTree(root, consumer, visitDirs, "*").- Parameters:
root- the root path to start at.consumer- the consumer accepting all files and directories.visitDirs- whether to visit subdirectories too or not.- Throws:
NullPointerException- if the given path or consumer is null.
-
walkFileTree
public static void walkFileTree(@NonNull @NonNull Path root, @NonNull @NonNull BiConsumer<Path, Path> consumer, boolean visitDirectories, @NonNull @NonNull String glob) Walks the file tree while filtering for the given glob and only visiting directories if specified. This starts at the given root path and passes the root directory together with the next file or directory into the bi consumer.- Parameters:
root- the root path to start at.consumer- the consumer accepting all files and directories.visitDirectories- whether to visit subdirectories too or not.glob- the glob pattern that each file has to match, use"*"to match everything.- Throws:
NullPointerException- if the given path, consumer or glob is null.
-
walkFileTree
public static void walkFileTree(@NonNull @NonNull Path root, @NonNull @NonNull BiConsumer<Path, Path> consumer, boolean visitDirectories, @NonNull DirectoryStream.Filter<Path> filter) Walks the file tree while filtering with the given filter and only visiting directories if specified. This starts at the given root path and passes the root directory together with the next file or directory into the bi consumer.- Parameters:
root- the root path to start at.consumer- the consumer accepting all files and directories.visitDirectories- whether to visit subdirectories too or not.filter- the filter to filter against.- Throws:
NullPointerException- if the given path, consumer or glob is null.
-
createDirectory
Creates all needed parent directories and the given directory only if the given path does not exist already. This method is equivalent toFiles.createDirectories(directoryPath)but catches thrown exceptions and just prints them into the error log.- Parameters:
directoryPath- the directory to create.
-
ensureChild
Ensures that the given child path is a child path of the given root path and throwing an exception otherwise.- Parameters:
root- the root path.child- the child path to check.- Throws:
IllegalStateException- if the child is not an actual child of the root path.NullPointerException- if the given root or child path is null.
-
resolve
Resolves the given array for the base path and returns the final path after all children are resolved one after another.- Parameters:
base- the base path to start at.more- all children to resolve onto the base path.- Returns:
- the resolved path.
- Throws:
NullPointerException- if the base path or of the children is null.
-