删除文件用
File. delete()
// 以下代码来自CSDN
/*
* 删除一个目录下的所有文件
*/
public static void delAllFile(String path) {
File file = new File(path);
if(!file.exists()) return;
if(!file.isDirectory()) return;
String[] tempList = file.list();
File temp = null;
for(int i = 0; i < tempList.length; i++) {
if(path.endsWith(File.separator))
temp = new File(path + tempList[i]);
else
temp = new File(path + File.separator + tempList[i]);
if(temp.isFile()) temp.delete();
if(temp.isDirectory()) delAllFile(path + tempList[i]);
}
}
File. delete()
// 以下代码来自CSDN
/*
* 删除一个目录下的所有文件
*/
public static void delAllFile(String path) {
File file = new File(path);
if(!file.exists()) return;
if(!file.isDirectory()) return;
String[] tempList = file.list();
File temp = null;
for(int i = 0; i < tempList.length; i++) {
if(path.endsWith(File.separator))
temp = new File(path + tempList[i]);
else
temp = new File(path + File.separator + tempList[i]);
if(temp.isFile()) temp.delete();
if(temp.isDirectory()) delAllFile(path + tempList[i]);
}
}
再对所有的子目录递归调用该函数
即
while(havefileinthisdirectory)
deletefiles()
foreach(dir in subdirectories)
call thisfunction(dir)