Add a helper to delete a folder

This commit is contained in:
Zontreck 2024-02-10 18:49:32 -07:00
parent 90468b8889
commit 8814702e6b

View file

@ -1,5 +1,6 @@
package dev.zontreck.ariaslib.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
@ -21,5 +22,30 @@ public class FileIO
} catch (IOException e) {
}
}
/**
* Recursively delete a directory
* @param directory The folder to delete
*/
public static void deleteDirectory(File directory) {
if (directory.exists()) {
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
file.delete();
}
}
}
// Now directory is empty, so delete it
directory.delete();
System.out.println("Directory deleted: " + directory.getAbsolutePath());
} else {
System.out.println("Directory does not exist: " + directory.getAbsolutePath());
}
}
}