mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-24 11:45:31 -04:00
Create FileUtils#deleteDirectory(Path) (#4482)
This commit is contained in:
parent
51954163d4
commit
a7178802f8
@ -23,6 +23,8 @@ import org.jackhuang.hmcl.util.Lang;
|
|||||||
import org.jackhuang.hmcl.util.StringUtils;
|
import org.jackhuang.hmcl.util.StringUtils;
|
||||||
import org.jackhuang.hmcl.util.function.ExceptionalConsumer;
|
import org.jackhuang.hmcl.util.function.ExceptionalConsumer;
|
||||||
import org.jackhuang.hmcl.util.platform.OperatingSystem;
|
import org.jackhuang.hmcl.util.platform.OperatingSystem;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.file.*;
|
import java.nio.file.*;
|
||||||
@ -188,6 +190,16 @@ public final class FileUtils {
|
|||||||
Files.write(file, data);
|
Files.write(file, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void deleteDirectory(Path directory) throws IOException {
|
||||||
|
if (!Files.exists(directory))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!Files.isSymbolicLink(directory))
|
||||||
|
cleanDirectory(directory);
|
||||||
|
|
||||||
|
Files.deleteIfExists(directory);
|
||||||
|
}
|
||||||
|
|
||||||
public static void deleteDirectory(File directory)
|
public static void deleteDirectory(File directory)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
if (!directory.exists())
|
if (!directory.exists())
|
||||||
@ -341,6 +353,35 @@ public final class FileUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void cleanDirectory(Path directory)
|
||||||
|
throws IOException {
|
||||||
|
if (!Files.exists(directory)) {
|
||||||
|
Files.createDirectories(directory);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Files.isDirectory(directory)) {
|
||||||
|
String message = directory + " is not a directory";
|
||||||
|
throw new IllegalArgumentException(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
Files.walkFileTree(directory, new SimpleFileVisitor<>() {
|
||||||
|
@Override
|
||||||
|
public @NotNull FileVisitResult visitFile(@NotNull Path file, @NotNull BasicFileAttributes attrs) throws IOException {
|
||||||
|
Files.delete(file);
|
||||||
|
return FileVisitResult.CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull FileVisitResult postVisitDirectory(@NotNull Path dir, @Nullable IOException exc) throws IOException {
|
||||||
|
if (!dir.equals(directory)) {
|
||||||
|
Files.delete(dir);
|
||||||
|
}
|
||||||
|
return FileVisitResult.CONTINUE;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public static void cleanDirectory(File directory)
|
public static void cleanDirectory(File directory)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
if (!directory.exists()) {
|
if (!directory.exists()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user