可以使用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"替换为要删除的目录的实际路径。该代码会递归地删除目录中的所有文件和子目录,直到目录为空,然后再删除该目录本身。