Cocos2dx FileUtils::getInstance()->removeDirectorys刪除文件夾,在Android上有些問題,。查看源碼之后發現實現是下面這樣:
bool FileUtils::removeDirectory(const std::string& path) { // FIXME: Why using subclassing? an interface probably will be better // to support different OS // FileUtils::removeDirectory is subclassed on iOS/tvOS // and system() is not available on tvOS #if !defined(CC_PLATFORM_IOS) if (path.size() > 0 && path[path.size() - 1] != '/') { CCLOGERROR("Fail to remove directory, path must terminate with '/': %s", path.c_str()); return false; } std::string command = "rm -r "; // Path may include space. command += "\"" + path + "\""; if (system(command.c_str()) >= 0) return true; else #endif return false; }
通過代碼看出,是用過 rm 移除。。。。
解決方案
1種使用lua方式實現 。參考 http://zengrong.net/post/2129.htm
第二種是使用Android的刪除。我這里使用的第二種
源碼奉上:
import java.io.File; //刪除文件夾和文件夾里面的文件 public static void deleteDir(final String pPath) { File dir = new File(pPath); deleteDirWihtFile(dir); } public static void deleteDirWihtFile(File dir) { if (dir == null || !dir.exists() || !dir.isDirectory()) return; for (File file : dir.listFiles()) { if (file.isFile()) file.delete(); // 刪除所有文件 else if (file.isDirectory()) deleteDirWihtFile(file); // 遞規的方式刪除文件夾 } dir.delete();// 刪除目錄本身 }
c++調用java ,lua調用c++這里就不說啦。