Create FileUtils#deleteDirectory(Path) (#4482)

This commit is contained in:
Glavo 2025-09-14 21:08:05 +08:00 committed by GitHub
parent 51954163d4
commit a7178802f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -23,6 +23,8 @@ import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.function.ExceptionalConsumer;
import org.jackhuang.hmcl.util.platform.OperatingSystem;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.*;
import java.nio.file.*;
@ -188,6 +190,16 @@ public final class FileUtils {
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)
throws IOException {
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)
throws IOException {
if (!directory.exists()) {