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

Included logger and format in FileUtil class

parent 8f2a3683
Pipeline #10396 passed with stage
in 17 seconds
Showing with 26 additions and 8 deletions
+26 -8
......@@ -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);
}
});
......
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