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")