cocos2dx lua 数据存储

最近项目中使用数据存储,需要存储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 data
end

-- 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()
    end
end

第二种 :用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) --  
        local dict = cc.FileUtils:getInstance():getValueMapFromFile(plistFile)  --  
        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 
    end

end 



下面是我的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")

    end
end

--读取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 data
end

-- 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()
    end
end


  -- 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) --  
        local dict = cc.FileUtils:getInstance():getValueMapFromFile(plistFile)  --  
        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 
    end

end 
return utilJson

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

 获得Documents路径方法

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




你可能感兴趣的:(cocos2dx lua 数据存储)