注意 f.delete() 这个方法的位置,一种是写在 else 里,一种是直接写在 if 后,这两个是有区别的,写在else里的时候只会删除文件,目录不会删除,
如果写在 if 外则会删除子目录(根目录不会删除)
public static void delFile(String dir) {
// TODO Auto-generated method stub
try {
File fdir = new File(dir);
File[] fs = fdir.listFiles();
if (fs != null) {
for (File f : fs) {
if (f.isDirectory()) {
delFile(f.getPath());
} else {
f.delete();
}
// f.delete();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}