PandaLib/pandalib-util/src/main/java/fr/pandacube/lib/util/FileUtils.java

58 lines
1.8 KiB
Java
Raw Normal View History

package fr.pandacube.lib.util;
2021-03-21 20:17:31 +01:00
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
2021-03-21 20:17:31 +01:00
/**
* Provides utility methods to manipulate files and directories
*/
2021-03-21 20:17:31 +01:00
public class FileUtils {
/**
* Recursively delete the provided file and all of its content if it is a directory.
* @param target the target file or directory.
*/
2021-03-21 20:17:31 +01:00
public static void delete(File target) {
if (target.isDirectory())
for (File child : target.listFiles())
delete(child);
target.delete();
}
/**
* Recursively copy the provided source file or directory to the provided target.
* @param source the source file or directory.
* @param target the copy destination.
* @throws IOException if an IO error occurs.
* @throws IllegalStateException if the target destination already exists and is not a directory.
* @throws IllegalArgumentException if at least one of the parameter is null, or if the source doesnt exists.
*/
2021-03-21 20:17:31 +01:00
public static void copy(File source, File target) throws IOException {
if (source == null || !source.exists()) {
throw new IllegalArgumentException("source is null or doesnt exists: " + source);
}
if (target == null) {
throw new IllegalArgumentException("target cannot be null");
}
2021-03-21 20:17:31 +01:00
if (target.exists() && !target.isDirectory()) {
throw new IllegalStateException("target file already exists: " + target);
}
BasicFileAttributes sourceAttr = Files.readAttributes(source.toPath(), BasicFileAttributes.class);
if (sourceAttr.isDirectory()) {
if (target.mkdir()) {
for (String child : source.list())
copy(new File(source, child), new File(target, child));
}
else {
throw new IOException("Cannot create directory " + target);
}
2021-03-21 20:17:31 +01:00
}
else if (sourceAttr.isRegularFile()) {
2021-03-21 20:17:31 +01:00
Files.copy(source.toPath(), target.toPath());
}
}
}