function readfile(path)
local file = io.open(path, "r")
if file then
local content = file:read("*a")
io.close(file)
return content
end
return nil
end
function writefile(path, content, mode)
mode = mode or "w+b"
local file = io.open(path, mode)
if file then
if file:write(content) == nil then return false end
io.close(file)
return true
else
return false
end
end
--文件讀寫測試
local a = {x=1,y=2,z=3}
local str = json.encode(a) --將lua表編碼為json格式字符串
local path = cc.FileUtils:getInstance():getWritablePath()
path = path..filename --得到可讀寫文件的路徑
writefile(path,str,r) --將數據寫入文件
local b = readfile(path) --從文件中讀取數據
printf(b)
end
2. 文件操作示例代碼
local path = cc.FileUtils:getInstance():getWritablePath()
cc.FileUtils:getInstance():writeToFile({key="value"}, paht.."filename") --讀取數據的方式
print(ret.key)
lcoal ret = cc.FileUtils:getInstance():getValueMapFromFile(Path.."filename") --讀取數據的方式2
print(str)
local str = cc.FileUtils:getInstance():getStringFromFile(path.."filename")