看好多人讨论txt文件的写法,我也分享个我自己的:
local file,data
local path = getSdPath().."/lrfolder"--文件夹路径
if fileExist(path)==false then--文件夹不存在则创建文件夹
mkdir(path)
end
path=path.."/file.txt"--文件路径
if fileExist(path)==false then--写文件,文件不存在则创建文件
file = io.open(path, "w+")--w+打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失,若文件不存在则建立该文件;a--以附加的方式打开只写文件
io.output(file)
io.write("0")--写入一个初始值
io.close(file)
else--读取文件
file = io.open(path, "r")--r以只读方式打开文件,该文件必须存在
io.input(file)
data=math.tointeger(io.read())--io.read()读第一行数据,若是读多行可以用io.lines
print("读取文件值:"..data)
io.close(file)
end
|