Commit 2dd4adee authored by mamunozgil's avatar mamunozgil
Browse files

Included logger and format in FileUtil class

No related merge requests found
Pipeline #10396 passed with stage
in 17 seconds
Showing with 26 additions and 8 deletions
+26 -8
...@@ -4,14 +4,18 @@ import java.io.File; ...@@ -4,14 +4,18 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; 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. * Created by Marcel Bochtler on 05.01.17.
*/ */
public class FileUtil { public class FileUtil {
private static final Logger LOG = LoggerFactory.getLogger(FileUtil.class);
/** /**
* Delete the folder and all containing files. * Delete the folder and all containing files.
* @param folder Folder to delete * @param folder Folder to delete
...@@ -23,22 +27,36 @@ public class FileUtil { ...@@ -23,22 +27,36 @@ public class FileUtil {
if (f.isDirectory()) { if (f.isDirectory()) {
deleteFolderRecursively(f); deleteFolderRecursively(f);
} else { } else {
f.delete(); if (f.delete()) {
System.out.println(String.format("Deleted file: %s", f.getAbsolutePath())); 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 { public static void copyFolder(Path src, Path dst) throws IOException {
Files.walk(src) Files.walk(src)
.forEach(source -> { .forEach(source -> {
try { try {
Files.copy(source, dst.resolve(src.relativize(source))); Path destination = dst.resolve(src.relativize(source));
System.out.println(String.format("Copying file from: %s to %s", source.toString(), dst.resolve(src.relativize(source)).toString())); Files.copy(source, destination);
LOG.debug("Copying file from: {} to {}", source, destination);
} catch (IOException e) { } catch (IOException e) {
LOG.debug("Error copying file from: {} to {}", source, dst.resolve(src.relativize(source)), e);
throw new RuntimeException(e.getMessage(), e); throw new RuntimeException(e.getMessage(), e);
} }
}); });
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment