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

39 lines
999 B
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
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);
}
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());
}
}
}