删除文件:
public boolean deleteFile(String sPath) {
boolean flag = false;
if(sPath == null){
return false;
}
File file = new File(sPath);
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
public boolean deleteDirectory(String sPath) {
if(sPath == null){
return false;
}
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
File[] files = dirFile.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag)
break;
}
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag)
break;
}
}
}
if (!flag)
return false;
if (dirFile.delete()) {
return true;
} else {
return false;
}
}