2022-07-20 13:18:57 +02:00
|
|
|
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;
|
2022-07-10 00:55:56 +02:00
|
|
|
import java.nio.file.attribute.BasicFileAttributes;
|
2021-03-21 20:17:31 +01:00
|
|
|
|
|
|
|
public class FileUtils {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void delete(File target) {
|
|
|
|
if (target.isDirectory())
|
|
|
|
for (File child : target.listFiles())
|
|
|
|
delete(child);
|
|
|
|
target.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void copy(File source, File target) throws IOException {
|
|
|
|
if (target.exists() && !target.isDirectory()) {
|
|
|
|
throw new IllegalStateException("target file already exists: " + target);
|
|
|
|
}
|
2022-07-10 00:55:56 +02:00
|
|
|
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
|
|
|
}
|
2022-07-10 00:55:56 +02:00
|
|
|
else if (sourceAttr.isRegularFile()) {
|
2021-03-21 20:17:31 +01:00
|
|
|
Files.copy(source.toPath(), target.toPath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|