io.close()
closes a file
io.flush()
flushes outstanding data to disk for the default output file
io.input()
opens filename for input in text mode
io.lines()
returns an iterator function for reading a named file line-by-line
io.open()
file = io.open (filename [, mode])
mode
- r 以只读方式打开文件,该文件必须存在。
- w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。
- a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。(EOF符保留)
- r+ 以可读写方式打开文件,该文件必须存在。
- w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。
- a+ 与a类似,但此文件可读可写
- b 二进制模式,如果文件是二进制文件,可以加上b
- +号表示对文件既可以读也可以写
io.output()
opens a file for output
io.popen()
creates a pipe and executes a command
io.read()
reads from the default input file
io.tmpfile()
returns a handle to a temporary file
io.type()
returns type of file handle
io.write()
writes to the default output file
file:close()
closes a file
file:flush()
向文件写入缓冲中的所有数据
file:lines()
returns an iterator function for reading the file line-by-line
file:read()
reads the file according to the specified formats
file:seek(optional whence, optional offset)
设置和获取当前文件位置,成功则返回最终的文件位置(按字节)
参数 whence 值可以是:
"set": 从文件头开始
"cur": 从当前位置开始[默认]
"end": 从文件尾开始
offset:默认为0
不带参数file:seek()则返回当前位置,file:seek("set")则定位到文件头,file:seek("end")则定位到文件尾并返回文件大小
file:setvbuf()
sets the buffering mode for an output file
file:write()
writes to a file
ok, message = os.remove (filename)
Deletes a file
ok, message = os.rename (oldname, newname)
Renames a file
s = os.tmpname ()
Returns a name for a temporary file