lua数据存储与文件解析

1、简单介绍:

cocos2dx-lua中对文件的操作都用cc.FileUtils这个类进行操作,它是单一实例。

访问res目录:(fullPathForFilename(arg))

local fullPathForFilename =cc.FileUtils:getInstance():fullPathForFilename(“text.txt”)。如果该文件存放在该目录的其他文件夹下,也要相应的加上搜索目录。

判断文件是否存在:(isFileExist(arg))

local isExist =cc.FileUtils:getInstance():isFileExist(“text.txt”)

可写入目录:(getWritablePath())

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

2、实际操作:

--测试资源目录信息

local sharedFileUtils = cc.FileUtils:getInstance()

local function onClickMenu1(pSender)

           localfullPathForFilename = sharedFileUtils:fullPathForFilename(“text.txt”)

           print(fullPathForFilename)            --如果res目录下,没有text.txt文件,那么输出为空

           localisExist = sharedFileUtils:isFileExist(“text.txt”)

           ifisExist == true then

                     print(“text.txtexists”)

           else

                     print(“text.txtdoesn’t exists”)

           end

end

--读文件

local fullPathForFilename =sharedFileUtils:fullPathForFilename(“test.txt”)

local content =sharedFileUtils:getStringFromFile(fullPathForFilename)

print(content)

--路径搜索

getSearchPaths()                             --获得所有搜索路径集合

setSearchPaths(searchPaths)      --设置搜索路径集合

addSearchPath(path)                                --追加搜索路径

例如在新建的工程中,在main.lua中最开始的位置:

cc.FileUtils:getInstance():addSearchPath("src/")

cc.FileUtils:getInstance():addSearchPath("res/")

sharedFileUtils:purgeCachedEntries()            --清理搜索文件缓存,一般是更新资源后进行搜索前调用。

local searchPaths = sharedFileUtils:getSearchPaths()             --获得搜索路径的容器,返回值是一个table。

local writablePath = sharedFileUtils:getWritablePath()            --获得可写目录

local resPrefix = “res/”

table.insert(searchPaths, 1, resPrefix..”dir2”)

table.insert(searchPaths, 1, resPrefix..”dir1”)

table.insert(searchPaths, 1, writablePath)

sharedFileUtils:setSearchPaths(searchPaths)                                       --设置搜索目录

3、UserDefault用法

获取单一实例:    localuserDefault = cc.UserDefault:getInstance()

常用方法:

local ret = userDefault:getStringForKey(“string”,“defaultString”)

userDefault:setStringForKey(“string”, “realString”)

getDoubleForKey、setDoubleForKey

getFloatForKey、setFloatForKey

getIntegerForKey、setIntegetForKey

getBoolForKey、setBoolForKey

4、字典列表结构的属性列表文件:

比如:

          

                     root

                    

                               

                                          date

                                          2008-08-16

                                          content

                                          初始化

                               

                               

                                          date

                                          2016-08-16

                                          content

                                          欢迎使用

                               

                    

          

那么读取上面数据:

local fileUtils = cc.FileUtils:getInstance()

local fullPathForFileName =fileUtils:fullPathForFilename(“notice.plist”)

local dict = fileUtils:getValueMapFromFile(fullPathForFileName)

for key, value in pairs(dict) do

           for i =1, table.getn(value) do

                     localrow = value[i]

                     localdate = row[“date”]

                     localcontent = row[“content”]

           end

end

5、访问根为列表结构的属性列表文件

          

                    

                                date

                                2010-08-04

                                content

                                初始化

                    

                    

                                date

                                2016-8-16

                                content

                                欢迎使用

                    

          

读取上面数据

local fileUtils = cc.FileUtils:getInstance()

local fullPathForFileName =fileUtils:fullPathForFilename(“notice.plist”)

local vector = fileUtils:getValueVectorFromFile(fullPathForFileName)

for i = 1, table.getn(vector) do

           local row= vector[i]

           localdate = row[“date”]

           localcontent = row[“content”]

end

6、json解析

json.decode(jsonStr)

json解码主要通过decode函数实现,将字符串转为json对象或json数组

local jsonStr = ‘[{“ID”:”1”, “CDate”:”19920715”, “Content”:”发布iOSBook0“},

                                 {“ID”:”2”, “CData”:”19920912”,“Content”:”发布iOSBook1”}]’

local jsonArray = json.decode(jsonStr)

for i = 1, table.getn(jsonArray) do

           localjsonObj = jsonArray[i]

           local ID= jsonObj[“ID”]

           localCDate = jsonObj[“CDate”]

           localContent = jsonObj[“Content”]

end

json.encode(jsonArray)

json编码主要通过encode函数实现,将json数组或对象转为字符串

local jsonObj = {ID=”1”, CDate=”19920715”, Content = “发布iOSBook0”}

local jsonStr = json.encode(jsonObj)

你可能感兴趣的:(lua数据存储与文件解析)