可以使用Java的File類和遞歸來刪除帶有內容的目錄。下面是一個示例代碼:
import java.io.File;
public class DeleteDirectoryExample {
public static void main(String[] args) {
File directory = new File("path/to/directory");
deleteDirectory(directory);
}
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();
}
}
}
directory.delete();
System.out.println("Directory deleted: " + directory.getAbsolutePath());
} else {
System.out.println("Directory not found: " + directory.getAbsolutePath());
}
}
}
需要將"path/to/directory"替換為要刪除的目錄的實際路徑。該代碼會遞歸地刪除目錄中的所有文件和子目錄,直到目錄為空,然后再刪除該目錄本身。