Class FileUtils
- java.lang.Object
-
- com.github.nosan.embedded.cassandra.commons.FileUtils
-
public final class FileUtils extends Object
A utility class providing simple methods for dealing with files and directories.- Since:
- 4.0.0
- Author:
- Dmytro Nosan
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Stringchecksum(Path file, String algorithm)Computes the checksum of the specified file using the given algorithm.static voidcopy(Path src, Path dest, CopyOption... options)Copies a file or directory to the target location.static voidcopy(Path src, Path dest, BiPredicate<? super Path,? super BasicFileAttributes> filter, CopyOption... options)Copies a file or directory to the target location with a filtering function.static booleandelete(Path path)Deletes the specifiedPath.
-
-
-
Method Detail
-
delete
public static boolean delete(Path path) throws IOException
Deletes the specifiedPath. If the path refers to a directory, it will recursively delete its contents before deleting the directory itself.This method is safe to use even if the specified path does not exist.
- Parameters:
path- the path to delete; ifnull, this method will returnfalse- Returns:
trueif the path existed and was deleted, orfalseif it did not exist- Throws:
IOException- if an I/O error occurs while deleting the path
-
copy
public static void copy(Path src, Path dest, CopyOption... options) throws IOException
Copies a file or directory to the target location. If the source path refers to a directory, it will be copied recursively, including its contents.- Parameters:
src- the source pathdest- the target pathoptions- the options specifying how the copy should be performed- Throws:
IOException- if an I/O error occurs during the copyNullPointerException- if any of the arguments arenull
-
copy
public static void copy(Path src, Path dest, BiPredicate<? super Path,? super BasicFileAttributes> filter, CopyOption... options) throws IOException
Copies a file or directory to the target location with a filtering function. If the source path refers to a directory, it will be copied recursively, but only files and directories that pass the filter will be included.- Parameters:
src- the source pathdest- the destination pathfilter- a predicate used to determine whether a file or directory should be copiedoptions- the options specifying how the copy should be performed- Throws:
IOException- if an I/O error occurs during the copyNullPointerException- if the source path, destination path, or options array isnull
-
checksum
public static String checksum(Path file, String algorithm) throws NoSuchAlgorithmException, IOException
Computes the checksum of the specified file using the given algorithm.This method reads the file's content and computes the hash using the provided algorithm (e.g.,
MD5,SHA-1,SHA-256). The result is returned as a lowercase hexadecimal string.- Parameters:
file- the file for which the checksum is computedalgorithm- the name of the algorithm to use for the hash computation- Returns:
- the computed checksum as a lowercase hexadecimal string
- Throws:
IOException- if an I/O error occurs while reading the fileNoSuchAlgorithmException- if the specified algorithm is not availableNullPointerException- if the file or algorithm isnull
-
-