/**
* 删除文件夹下的所有文件
* @param oldPath
*/
public static void deleteFile(File oldPath) {
if (oldPath.isDirectory()) {
System.out.println(oldPath + "是文件夹--");
File[] files = oldPath.listFiles();
for (File file : files) {
deleteFile(file);
}
} else {
oldPath.delete();
}
}
/**
* 递归删除目录下的所有文件及子目录下所有文件,包括当前目录
* @param dir 将要删除的文件目录
* @return
*/
private static boolean deleteDir(File dir) {
if (!dir.exists()) {
return false;
}
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
boolean success = dir.delete();
return success;
}
Java删除文件夹以及文件
最新推荐文章于 2023-07-26 14:59:31 发布