How to delete a temporary folder created in Java?
In Java, you can use the following method to delete a temporary folder:
import java.io.File;
public class DeleteTemporaryFolder {
public static void main(String[] args) {
// 创建临时文件夹
File tempFolder = new File("path/to/temp/folder");
// 删除临时文件夹
deleteFolder(tempFolder);
}
public static void deleteFolder(File folder) {
if (folder.exists()) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
// 递归删除子文件夹
deleteFolder(file);
} else {
// 删除文件
file.delete();
}
}
}
// 删除文件夹
folder.delete();
}
}
}
The code will recursively delete all files and folders in the temporary folder and its subfolders, and finally delete the root folder itself. Please replace “path/to/temp/folder” in the code with the actual path to the temporary folder.