最近写一个功能,需要写入本地缓存文件,记录一下Lua的IO用法:
local checkClickPath = Application.persistentDataPath.."/CheckClick.lua"
function MafiaPrequelProxy:CheckRoleClick(uId)--检测玩家id是否点击过
local userStr = tostring(uId).."=false;"
local newStr = ""
file = io.open(checkClickPath, "r")
if file ~= nil then--存在文件
io.input(file)
ioStr = io.read()
io.close(file)
local roleDataList = string.split(ioStr, ";")--玩家账号信息列表
local isPlayerInList = false
local isPlayerClick = false
for i = 1,#roleDataList do
local rData = string.split(roleDataList[i], "=")
local rId = rData[1]
local isClick = rData[2]
if rId == tostring(uId) then
isPlayerInList = true
if isClick == "true" then
isPlayerClick = true
end
break
end
end
if isPlayerInList then--玩家信息在文件中
newStr = ioStr
return isPlayerClick, newStr
else--玩家不在文件中,写入
newStr = ioStr..userStr
file = io.open(checkClickPath,"w+")
io.output(file)
file:write(newStr)
io.close(file)
end
else--文件不存在
file = io.open(checkClickPath,"w+")
io.output(file)
file:write(userStr)
io.close(file)
newStr = userStr
end
return false, newStr
end