1. 程式人生 > >lua資料儲存與檔案解析

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、字典列表結構的屬性列表檔案:

比如:

<plist version=”1.0”>

           <dict>

                     <key>root</key>

                     <array>

                                <dict>

                                          <key>date</key>

                                          <string>2008-08-16</string>

                                          <key>content</key>

                                          <string>初始化</string>

                                </dict>

                                <dict>

                                          <key>date</key>

                                          <string>2016-08-16</string>

                                          <key>content</key>

                                          <string>歡迎使用</string>

                                </dict>

                     </array>

           </dict>

</plist>

那麼讀取上面資料:

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、訪問根為列表結構的屬性列表檔案

<plist version = “1.0”>

           <array>

                     <dict>

                                <key>date</key>

                                <string>2010-08-04</string>

                                <key>content</key>

                                <string>初始化</string>

                     </dict>

                     <dict>

                                <key>date</key>

                                <string>2016-8-16</string>

                                <key>content</key>

                                <string>歡迎使用</string>

                     </dict>

           </array>

</plist>

讀取上面資料

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)