cocos2dx lua 数据存储

来源:互联网 发布:数控镗床编程 编辑:程序博客网 时间:2024/05/20 04:29

最近项目中使用数据存储,需要存储table表,不能简单的userDefault来存储,也不合适(这里就不记录了,网上多的是)

第一种 :用json文件存储 

     实际上用io操作,封装成了一个方法

--读取json文件utilJson.LoadFile = function(filename)    local file    if filename == nil then        file = io.stdin    else        local err        file, err = io.open(filename, "rb")        if file == nil then            error(("Unable to read '%s': %s"):format(filename, err))        end    end    local data = file:read("*a")    if filename ~= nil then        file:close()    end    if data == nil then        error("Failed to read " .. filename)    end    data = json.decode(data)    return dataend

-- Function 保存文件utilJson.SaveFile = function(filename, data) --filename 全路径    data为lua中的table    print("filename",filename)    local file    if filename == nil then        file = io.stdout    else        local err        file, err = io.open(filename, "wb")        if file == nil then            error(("Unable to write '%s': %s"):format(filename, err))        end    end    data = json.encode(data)    file:write(data)    if filename ~= nil then        file:close()    endend

第二种 :用plist文件存储 

    这种我只做了读取,写的方法还有点问题,写入成功了但是值好像没保存下来(以后改正)


  --读取plist文件utilJson.getPlistDict = function(fileName)        print("是否存在",cc.FileUtils:getInstance():isFileExist(fileName))      if cc.FileUtils:getInstance():isFileExist(fileName) then        local plistFile = cc.FileUtils:getInstance():fullPathForFilename(fileName)          -- local dict = cc.FileUtils:getInstance():getValueVectorFromFile(plistFile) --<array>          local dict = cc.FileUtils:getInstance():getValueMapFromFile(plistFile)  --<dict>          return dict       end end 

--写入plist文件utilJson.savePlistDict = function(tbl,fileName)     print("是否存在",cc.FileUtils:getInstance():isFileExist(fileName))    if cc.FileUtils:getInstance():isFileExist(fileName) then      local dict = {}      dict["data"] = tbl      for k,v in pairs(tbl[1]) do          print(k,v)      end      local reslut = cc.FileUtils:getInstance():writeValueMapToFile(dict,fileName)        print("存成功",reslut)       return reslut     endend 



下面是我的utilJson.lua文件

local utilJson = {}-- json工具local cJson = require("cjson") utilJson.decode = function (fileName)    print("参数",fileName)local filePath = fileName    local isExist = cc.FileUtils:getInstance():isFileExist(filePath)    print(isExist)    local t = cc.FileUtils:getInstance():getStringFromFile(filePath)        if nil ~= t and "" ~= t then        local jsonData = cJson.decode( t )        if jsonData == nil then            print("Json error")                    end        return jsonData    else        print("data was empty")    endend--读取json文件utilJson.LoadFile = function(filename)    local file    if filename == nil then        file = io.stdin    else        local err        file, err = io.open(filename, "rb")        if file == nil then            error(("Unable to read '%s': %s"):format(filename, err))        end    end    local data = file:read("*a")    if filename ~= nil then        file:close()    end    if data == nil then        error("Failed to read " .. filename)    end    data = json.decode(data)    return dataend-- Function 保存文件utilJson.SaveFile = function(filename, data) --filename 全路径    data为lua中的table    print("filename",filename)    local file    if filename == nil then        file = io.stdout    else        local err        file, err = io.open(filename, "wb")        if file == nil then            error(("Unable to write '%s': %s"):format(filename, err))        end    end    data = json.encode(data)    file:write(data)    if filename ~= nil then        file:close()    endend  -- local tmpTable = shopLayer.getPlistDict("data/buyPersonBall.plist")  -- for _,v in pairs(tmpTable) do  --     for i,j in pairs(v) do  --         print(i,j)  --     end  -- end  -- tmpTable[1]["2"] = 5  -- shopLayer.savePlistDict(tmpTable,"data/buyPersonBall.plist")  -- tmpTable = shopLayer.getPlistDict("data/buyPersonBall.plist")  -- print("改变后的值",tmpTable[1]["2"])  --读取plist文件utilJson.getPlistDict = function(fileName)        print("是否存在",cc.FileUtils:getInstance():isFileExist(fileName))      if cc.FileUtils:getInstance():isFileExist(fileName) then        local plistFile = cc.FileUtils:getInstance():fullPathForFilename(fileName)          -- local dict = cc.FileUtils:getInstance():getValueVectorFromFile(plistFile) --<array>          local dict = cc.FileUtils:getInstance():getValueMapFromFile(plistFile)  --<dict>          return dict       end end --写入plist文件utilJson.savePlistDict = function(tbl,fileName)     print("是否存在",cc.FileUtils:getInstance():isFileExist(fileName))    if cc.FileUtils:getInstance():isFileExist(fileName) then      local dict = {}      dict["data"] = tbl      for k,v in pairs(tbl[1]) do          print(k,v)      end      local reslut = cc.FileUtils:getInstance():writeValueMapToFile(dict,fileName)        print("存成功",reslut)       return reslut     endend return utilJson

注意:在ios系统下 数据只能存放在Documents路径下 才能允许读取并写入

 获得Documents路径方法

local writablePath = cc.FileUtils:getInstance():getWritablePath()




0 0
原创粉丝点击