可以使用Java的File類和遞歸來刪除帶有內(nèi)容的目錄。下面是一個(gè)示例代碼:
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"替換為要?jiǎng)h除的目錄的實(shí)際路徑。該代碼會(huì)遞歸地刪除目錄中的所有文件和子目錄,直到目錄為空,然后再刪除該目錄本身。