public class DeleteFileAndDir {
/**
* 递归删除给定文件
*
* @param file
* 文件名
*/
public static void delete(File file) {
if (!file.exists()) {
return;
}
if (file.isFile()) {
file.delete();
}
if (file.isDirectory()) {
File[] childFiles = file.listFiles();
if (childFiles == null || childFiles.length == 0) {
file.delete();
return;
}
for (int i = 0; i < childFiles.length; i++) {
delete(childFiles[i]);
}
file.delete();
}
}
}
本文介绍了一个Java工具类,用于递归删除指定的文件或目录。该工具类支持单个文件的删除以及整个目录及其子目录的完全清理。
612

被折叠的 条评论
为什么被折叠?



