Java 删除某个目录下面所有的文件
File codeDir = new File("C:\\111111");
deleteContents(codeDir); // 删除 111111 目录下面所有内容,但不删除 111111这个目录
deleteContentsAndDir(codeDir); // 删除 111111 目录下面所有内容,最后将 111111这个目录也删除了
public static boolean deleteContentsAndDir(File dir) {
if (deleteContents(dir)) {
return dir.delete();
} else {
return false;
}
}
public static boolean deleteContents(File dir) {
File[] files = dir.listFiles();
boolean success = true;
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
success &= deleteContents(file);
}
if (!file.delete()) {
System.out.println("Failed to delete " + file);
success = false;
}
}
}
return success;
}
标签:Java,File,删除,111111,file,某个,目录,dir From: https://www.cnblogs.com/onelikeone/p/17445293.html