lua文件读写操作

来源:互联网 发布:战网客户端mac版下载 编辑:程序博客网 时间:2024/06/05 20:36

--读取文件操作

local f = assert(io.open("config.txt",'r'))--[[r表示读取的权限(read),a表示追加(append),w表示写的权限(write),b表示打开二进制(binary)]]

local string = f:read("*all")--[[*all表示读取所有的文件内容,*line表示读取一行,*number读取一个数字,<num>表示读取一个不超过num长度的字符]]

f:close() --关闭流

print(string)

--封装成函数

local function read_files( fileName )

local f = assert(io.open(fileName,'r'))

local content = f:read("*all")f:close()

return contentend 

local rlt = read_files("config.txt")

print(rlt)

local f = assert(io.open("ok.txt",'w'))f:write("welcome to lua\n cocos2d-x")f:write("aaaaaaaaa")f:close()local function write_content( fileName,content )local  f = assert(io.open(fileName,'a'))f:write(content)f:close()endwrite_content("ok.txt","hello lua\n")local long_string = [[\n cocos2d-x and lua welcome to you]]write_content("ok.txt", long_string)
原创粉丝点击