这是我在实际的项目中遇见的问题,备份一下代码,以后用到直接拿来用。我就额不细说了,很简单,就是File的操作。直接上代码。我相信可以看明白的。
public void deleteUserDataFolder(String datapath) {
try {
deleteAllFile(datapath);
String filePath = datapath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
myFilePath.delete();
} catch (Exception e) {
//日志
}
}
private void deleteAllFile(String datapath) {
File file = new File(datapath);
if (!file.exists()) {
return ;
}
if (!file.isDirectory()) {
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (datapath.endsWith(File.separator)) {
temp = new File(datapath + tempList[i]);
} else {
temp = new File(datapath + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
deleteAllFile(datapath + "/" + tempList[i]);
deleteUserDataFolder(datapath + "/" + tempList[i])
}
}
return;
}