美文网首页
Cocos2dx 文件的读写

Cocos2dx 文件的读写

作者: 游戏创作者 | 来源:发表于2020-09-19 15:04 被阅读0次

Cocos2dx-lua 文件读写例子:


local ChatDataCache = class("ChatDataCache")

function ChatDataCache:instance()
    if not self._instance then
        self._instance = ChatDataCache.new() 
    end
    
    return self._instance
end 

function ChatDataCache:ctor( ... )

    self._fileUtils = cc.FileUtils:getInstance()
    self.directory = device.writablePath .. device.directorySeparator.. "ChatCache"
    if not self._fileUtils:isDirectoryExist(self.directory) then
        self._fileUtils:createDirectory(self.directory)
    end

    print("+++++self.directory:",self.directory)
end

function ChatDataCache:saveFileByName( jsonStr,name )
    local fullPath = self.directory..device.directorySeparator..name..".json"

    if self._fileUtils:isFileExist(fullPath) then
        self._fileUtils:removeFile(fullPath)
    end

    local ok = self._fileUtils:writeStringToFile(jsonStr,fullPath)
    if ok then
        print("++++++++保存成功")
    end
end

function ChatDataCache:getFileDataByName( name )
    local fullPath = self.directory .. device.directorySeparator..name..".json"
    if self._fileUtils:isFileExist(fullPath) then
        local jsonStr = self._fileUtils:getStringFromFile(fullPath)
        local data = json.decode(jsonStr)
        return data
    end
    return {}
end

function ChatDataCache:getAllFile( ... )
    -- body
end

return ChatDataCache

这里用了cocos官方提供的 FileUtils 去读写文件的,他还提供了很多API,详细请看源码。

相关文章

  • Cocos2dx 文件的读写

    Cocos2dx-lua 文件读写例子: 这里用了cocos官方提供的 FileUtils 去读写文件的,他还提供...

  • Cocos2dx lua Xcode Mac

    Cocos2dx lua Xcode Mac 1. 下载Cocos2dx 相关文件 2. 新建Cocos2dX l...

  • iOS制作cocos2dx lua SDK

    初识cocos2dx框架cocos2dx项目结构(cocos2dx 3.17) 原生SDK OC语言的桥接文件 l...

  • C语言读写文件

    C语言文件读写### 标准文件读写 非标准文件读写 标准文件读写 头文件 include 打开文件 函数原型:FI...

  • 跟我一起学Python(八)

    一、IO编程 读写文件是最常见的IO操作,Python内置了读写文件的函数。文件读写的原理:在磁盘上读写文件的功能...

  • Python 学习笔记6 2018-04-13

    文件操作: 1,文件的读写操作 2,文件的各种系统操作 3,存储对象 1,文件的读写操作 读写数据: ...

  • 用Python实现磁盘IO操作全攻略,让数据流动起来!

    01 文件读写 1. 打开文件 读写文件是最常见的IO操作。Python内置了读写文件的函数,方便了文件的IO操作...

  • 文件操作导航

    文件打开与关闭文件读写文件的定位读写文件的重命名、删除文件夹的相关操作

  • python学习笔记03

    文件处理 读写文件是最常见的IO操作。Python内置了读写文件的函数,用法和C是兼容的。 读写文件前,我们先必须...

  • Python学习_IO文件操作

    在编程工作中,时常需要对各种文件进行操作。读写文件是最常见的IO编程,Python中内置了读写文件的函数。读写文件...

网友评论

      本文标题:Cocos2dx 文件的读写

      本文链接:https://www.haomeiwen.com/subject/drieyktx.html