解決的辦法:
/**
* 刪除文件夾
* @param filePathAndName String 文件夾路徑及名稱 如c:/fqf
* @param fileContent String
* @return boolean
*/
public void delFolder(String folderPath) {
try {
delAllFile(folderPath); //刪除完裏面所有內容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); //刪除空文件夾
}
catch (Exception e) {
System.out.println("刪除文件夾操作出錯");
e.printStackTrace();
}
}
/**
* 刪除文件夾裏面的所有文件
* @param path String 文件夾路徑 如 c:/fqf
*/
public 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]);//先刪除文件夾裏面的文件
delFolder(path+"/"+ tempList[i]);//再刪除空文件夾
}
}
}
/**
* 刪除指定目錄下文件及目錄
*
* @param deleteThisPath
* @param filepath
* @return
*/
public void deleteFolderFile(String filePath, boolean deleteThisPath)
throws IOException {
if (!TextUtils.isEmpty(filePath)) {
File file = new File(filePath);
if (file.isDirectory()) {// 處理目錄
File files[] = file.listFiles();
for (int i = 0; i < files.length; i++) {
deleteFolderFile(files[i].getAbsolutePath(), true);
}
}
if (deleteThisPath) {
if (!file.isDirectory()) {// 如果是文件,刪除
file.delete();
} else {// 目錄
if (file.listFiles().length == 0) {// 目錄下沒有文件或者目錄,刪除
file.delete();
}
}
}
}
}
刪除系統中的一個文件夾,及內容
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.