-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtils.lua
More file actions
98 lines (86 loc) · 4.01 KB
/
Copy pathFileUtils.lua
File metadata and controls
98 lines (86 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
--old version
CCUserDefault:sharedUserDefault():setBoolForKey("MusicSwitch",true)
--new vestion
--FileUtils
local sharedFileUtils = cc.FileUtils:getInstance()--获取实例
--获取文件路径fullPathForFilename = D:/HelloLua/HelloLua/res/text.txt
local fullPathForFilename = sharedFileUtils:fullPathForFilename("test.txt")
--判断文件是否存在
local isExist = sharedFileUtils:isFileExist("test.txt")--参数是路径
--获取文件内容
local content = sharedFileUtils:getStringFromFile(fullPathForFilename)--fullPathForFilename先获取路径
label:setPosition(cc.p(100,100))
sharedFileUtils:purgeCachedEntries()--清理文件查找缓存,一般是更新资源后,进行搜索前调用
--获取所有搜索路径,searchPaths是个table
local searchPaths = sharedFileUtils:getSearchPaths()
--获得可写入目录 writablePath = D:\HelloLua\HelloLua\
local writablePath = sharedFileUtils:getWritablePath()
local resPrefix = "res/"
table.insert(searchPaths,1,resPrefix.."dir2")
--先插入新增,重新设置搜索路径
sharedFileUtils:setSearchPaths(searchPaths)
--追加路径
sharedFileUtils:addSearchPath("res/fonts")--必须在setSearchPaths后添加
--PLIST
--根为字典结构的属性列表
local sharedFileUtils = cc.FileUtils:getInstance()--获取实例
local fullPathForFilename = sharedFileUtils:fullPathForFilename("NotesList.plist")
local dict = sharedFileUtils:getValueMapFromFile(fullPathForFilename)
for key,value in pairs(dict) do
for i = 1, table.getn(value) do--
cclog("--------%d--------",i)
local row = value[i]
local date = row["date"]
local content = row["content"]
cclog("date = %s", date)
cclog("content : %s", content)
end
end
--根为列表结构的属性列表
local vector = sharedFileUtils:getValueVectorFromFile(fullPathForFilename)
for i = 1, table.getn(vector) do
cclog("--------%d--------",i)
local row = vector[i]
local date = row["date"]
local content = row["content"]
cclog("date = %s", date)
cclog("content : %s", content)
end
--XML保存游戏设置,和精灵状态等
local defaults = cc.UserDefault:getInstance()
--根据键获取布尔值,第二个参数可选,如果第一个参数的值不存在,返回defaultValue
local ret = cc.UserDefault:getInstance():getBoolForKey("bool", true)--注意值不要加“”
local ret = cc.UserDefault:getInstance():getIntegerForKey("first", "11")
local ret = cc.UserDefault:getInstance():getFloatForKey("second", "1.1")
local ret = cc.UserDefault:getInstance():getDoubleForKey(pKey, defaultValue)
local ret = cc.UserDefault:getInstance():getStringForKey(pKey, defaultValue)
local ret = cc.UserDefault:getInstance():getBoolForKey(pKey, defaultValue)
--储存函数
cc.UserDefault:getInstance():setStringForKey("string","value2")
cc.UserDefault:getInstance():setIntegerForKey("integer","11")
cc.UserDefault:getInstance():setFloatForKey("float","2.5")
cc.UserDefault:getInstance():setDoubleForKey("double","2.6")
cc.UserDefault:getInstance():setBoolForKey("bool",false)
--数组
--解码
local jsonStr = '[{"ID":"1","CDate":"2012-12-23","Content":"发布iOSBook0"},{"ID":"2","CDate":"2012-12-24","Content":"发布iOSBook1"}]'
local jsonObj = json.decode(jsonStr)
for k,v in pairs(jsonObj) do
cclog("ID:%s",v["ID"])
cclog("CDate:%s",v["CDate"])
cclog("Content:%s",v["Content"])
end
--加密
local jsonArray = {{ID="1",CDate="2012-12-23",Content="发布iOSBook0"},
{ID="2",CDate="2012-12-24",Content="发布iOSBook1"}}
cclog("jsonArray : %s",json.encode(jsonArray))
--对象
--解码
local jsonStr = '{"ID":"1","CDate":"2012-12-23","Content":"发布iOSBook0"}'
local jsonObj = json.decode(jsonStr)
cclog("ID:%s",jsonObj["ID"])
cclog("CDate:%s",jsonObj["CDate"])
cclog("Content:%s",jsonObj["Content"])
--加密
local jsonOb = {ID="1",CDate="2012-12-23",Content="发布iOSBook0"}
cclog("jsonOb : %s",json.encode(jsonOb))