--I/O库为文件操作提供2个里一个输入库和一个输出库io.read() --io.write() 该函数将所有参数按照顺序写到当前输出文件中 FILE_NAME = 'c:/1.txt' FILE_NAME2 = ' ' function write() io.write('hello ', 'world') end --write() --io.read() 读取当前文件的内容 "*all" "*line" "*number" number --[[for count = 1,math.huge do local line = io.read("*line") --如果不传参数,缺省值也是"*line" if line == nil then break end io.write(string.format("%6d ",count),line,"\n") end--]] --读取指定文件 function getFile(file_name) local f = assert(io.open(file_name, 'r')) local string = f:read("*all") f:close() return string end -- local lines,rest = f:read(BUFSIZE,"*line") function getFileLine(file_name) local BUFSIZE = 84012 local f = assert(io.open(file_name, 'r')) local lines,rest = f:read(BUFSIZE, "*line") f:close() return lines , rest end --字符串写入 function writeFile(file_name,string) local f = assert(io.open(file_name, 'w')) f:write(string) f:close() end writeFile(FILE_NAME2, getFile(FILE_NAME))
--控制台写入字符串到文件中 function writeFile2(file_name) local f = assert(io.open(file_name, 'w')) local string = io.read() f:write(string) f:close() end io.write() writeFile2(FILE_NAME2)