文件处理
直接使用io调用
io.close();文件流关闭
io.flush();如果文件流以bufferd缓存模式处理,输入不会立即存入文件,需要调用本函数
io.input(file);输入
io.open(filename,mode);以相应的模式打开文件流.
io.output(file)输出文件
io.tmpfile()打开一个临时文件流
io.type(obj) 类型
io.write(); 写入文件
明确调用
file:close();//关闭文件流
file = io.open("file.txt","w");
file:write("data data data...");
file:close();
file:read();//*n 数字 *a 全部 *l 按行 number
local filename = "data_test.txt";
local file = io.open(filename);
local contents = file:read("*a");
file:close();
print(""Contents: ",contens);
file:seek(whence,offset);
//"set","cur","end"
文件设置或者得到当前文件字符指针位置
file:setbuf(mode,size);
io.output():setbuf("no");//no 无buf full 全buf line 一行行
file:write(); 写入
保存变量
function writetofilesingle(value,filename)
local hfile = io.open("filename","w");
if hfile==nil then return end
hfile:write(value);
file:close();
end
获得数据
function readfromfilesingle(filename)
local hfile = io.open("filename","r");
if hfile == nil then return end;
local value = hfile:read("*a")
hfile:close();
return value;
end
文件流打开后,有固定的属性lines();可以遍历
local results = {}
local hfile = io.open(filename);
for line in hfile:lines() do
table.insert(results,line);
end
全局变量
myvar =1
print (_G["myvar"]);
处理JSON
require(“json”);
json.encode()
json.decode();