From 2dd4adeebdc17620911898b3b2adc84a3b75ea24 Mon Sep 17 00:00:00 2001 From: mamunozgil <miguel.munoz-gil@hft-stuttgart.de> Date: Mon, 25 Nov 2024 13:22:42 +0100 Subject: [PATCH] Included logger and format in FileUtil class --- .../dtabackend/utils/FileUtil.java | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/hftstuttgart/dtabackend/utils/FileUtil.java b/src/main/java/de/hftstuttgart/dtabackend/utils/FileUtil.java index f185b9c..33404e3 100644 --- a/src/main/java/de/hftstuttgart/dtabackend/utils/FileUtil.java +++ b/src/main/java/de/hftstuttgart/dtabackend/utils/FileUtil.java @@ -4,17 +4,21 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** - * Helper Class for all file related tasks. + * Helper Class for all file-related tasks. * * Created by Marcel Bochtler on 05.01.17. */ public class FileUtil { + private static final Logger LOG = LoggerFactory.getLogger(FileUtil.class); + /** * Delete the folder and all containing files. - * @param folder Folder to delete + * @param folder Folder to delete */ public static void deleteFolderRecursively(File folder) { File[] files = folder.listFiles(); @@ -23,22 +27,36 @@ public class FileUtil { if (f.isDirectory()) { deleteFolderRecursively(f); } else { - f.delete(); - System.out.println(String.format("Deleted file: %s", f.getAbsolutePath())); + if (f.delete()) { + LOG.debug("Deleted file: {}", f.getAbsolutePath()); + } else { + LOG.debug("Failed to delete file: {}", f.getAbsolutePath()); + } } } } - folder.delete(); - System.out.println(String.format("Deleted folder: %s", folder.getAbsolutePath())); + if (folder.delete()) { + LOG.debug("Deleted folder: {}", folder.getAbsolutePath()); + } else { + LOG.debug("Failed to delete folder: {}", folder.getAbsolutePath()); + } } + /** + * Copy the folder and its contents. + * @param src Source folder path + * @param dst Destination folder path + * @throws IOException if an I/O error occurs + */ public static void copyFolder(Path src, Path dst) throws IOException { Files.walk(src) .forEach(source -> { try { - Files.copy(source, dst.resolve(src.relativize(source))); - System.out.println(String.format("Copying file from: %s to %s", source.toString(), dst.resolve(src.relativize(source)).toString())); + Path destination = dst.resolve(src.relativize(source)); + Files.copy(source, destination); + LOG.debug("Copying file from: {} to {}", source, destination); } catch (IOException e) { + LOG.debug("Error copying file from: {} to {}", source, dst.resolve(src.relativize(source)), e); throw new RuntimeException(e.getMessage(), e); } }); -- GitLab