http://www.lua.org/manual/5.1/
0.读写种类
r 读取模式
w 写入模式(覆盖现有内容)
a 附加模式(附加在现有内容之后)
b 二进制模式
r+ 读取更新模式(现有数据保留)
w+ 写入更新模式(现有数据擦除)
a+ 附加更新模式(现有数据保留,只在文件末尾附加)
1.一些函数
assert(file); 找不到文件抛出异常
2.读写文件
path = "/Users/admin/Desktop/file.rtf"
function fileReadSave()
local file = io.open(path,"r");
assert(file);
local data = file:read("*a"); -- 读取所有内容
print(data)
for l in file:lines() do --一行一行读取
@H_502_84@print(l)
end
file:close();
file = io.open(path,"w");
assert(file);
file:write("dataa的速度a11\n"); -- \n 一行一行写进去
file:write("dataaa11\n");
file:write("dataaa11\n");
file:write("dataaa11\n");
file:close();
end
3.读写 table
参考
cha = {}; cha[1] = { basic = { name = "农民", cha_type = 1, }, combat = { acquire = 600.00, basic_def = 10, },}; function SavetableContent(file,obj) local szType = type(obj); print(szType); if szType == "number" then file:write(obj); elseif szType == "string" then file:write(string.format("%q",obj)); elseif szType == "table" then --把table的内容格式化写入文件 file:write("{\n"); for i,v in pairs(obj) do file:write("["); SavetableContent(file,i); file:write("]=\n"); SavetableContent(file,v); file:write(",\n"); end file:write("}\n"); else error("can't serialize a "..szType); end end function Savetable() local file = io.open(path,"w"); assert(file); file:write("cha = {}\n"); file:write("cha[1] = \n"); SavetableContent(file,cha[1]); file:write("}\n"); file:close(); end Savetable();
总结以上是内存溢出为你收集整理的lua文件读写全部内容,希望文章能够帮你解决lua文件读写所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)