Android刪除文件夾(文件夾以及文件夾下所有的文件)


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++這里就不說啦。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM