lua io操作
io 操作
相关函数
io.close(file):关闭文件,file可选,如果没有指定,关闭默认的文件
io.flush():刷新文件
io.input ([file]):设置默认输入文件,io.read从文件中读取内容
io.output ([file]):设置默认的输出文件,io.write向文件中写入内容
io.write (···):向默认文件中写入内容,...为字符串(写入文件的内容)
io.lines ([filename, ···]):以可读模式打开文件,每次返回一行数据
当到文件尾时,将返回nil,但不关闭文件
如不指定filename,则从控制态读取数据
io.read(format):读取文件内容的格式
Reads the file file, according to the given formats, which specify what to read.
For each format, the function returns a string or a number with the characters
read, or fail if it cannot read data with the specified format. (In this latter
case, the function does not read subsequent formats.) When called without
arguments, it uses a default format that reads the next line (see below).
* 根据指定的格式读取文件内容
The available formats are
"n": reads a numeral and returns it as a float or an integer, following the
lexical conventions of Lua. (The numeral may have leading whitespaces and a sign.)
This format always reads the longest input sequence that is a valid prefix for a
numeral; if that prefix does not form a valid numeral (e.g., an empty string, "0x",
or "3.4e-") or it is too long (more than 200 characters), it is discarded and the
format returns fail.
"a": reads the whole file, starting at the current position. On end of file, it
returns the empty string; this format never fails.
"l": reads the next line skipping the end of line, returning fail on end of
file. This is the default format.
"L": reads the next line keeping the end-of-line character (if present),
returning fail on end of file.
number: reads a string with up to this number of bytes, returning fail on end
of file. If number is zero, it reads nothing and returns an empty string, or fail
on end of file.
* n:读取一个数字并返回它。例:io.read("n")
* a:从当前位置读取整个文件。例:io.read("a")
* l:读取下一行,跳过文件结束符,在文件尾 (EOF) 处返回 nil。例:io.read("l")
* L:读取下一行,保留文件结束符,在文件尾 (EOF) 处返回 nil。例:io.read("L")
* number:返回一个指定字符个数(字节单位)的字符串,或在 EOF 时返回 nil。例:io.read(5)
The formats "l" and "L" should be used only for text files.
* l、L只应该用在文本文件中
io.open(filename, mode):按照指定模式打开文件,mode可选
# io.open (filename [, mode]):按照指定模式打开文件
This function opens a file, in the mode specified in the string mode.
In case of success, it returns a new file handle.
* 该函数以指定模式打开文件,返回一个新的文件句柄
The mode string can be any of the following:
"r": read mode (the default);
"w": write mode;
"a": append mode;
"r+": update mode, all previous data is preserved;
"w+": update mode, all previous data is erased;
"a+": append update mode, previous data is preserved,
writing is only allowed at the end of file.
* r:以只读形式打开文件(默认)
* w:以致谢形式打开文件
* a:以追加模式打开文件
* r+:更新模式(可读写),之前数据会保留
* w+:更新模式(可读写),之前所有数据会清空
* a+:追加更新模式(可读写),之前所有数据会保留
* +:表示既可以读,也可以写
The mode string can also have a 'b' at the end, which is needed
in some systems to open the file in binary mode.
* 如果模式以b结尾,表示以2进制形式打开二进制文件
io.tmpfile ():返回一个临时文件句柄,该文件以更新模式打开,程序结束时自动删除
# io.tmpfile ():返回一个临时文件句柄,该文件以更新模式打开,程序结束时自动删除
In case of success, returns a handle for a temporary file.
This file is opened in update mode and it is automatically
removed when the program ends.
io.type(obj):检查是否是一个有效的文件句柄
# io.type (obj):检查是否是一个有效的文件句柄,返回:file、closed file、fail
Checks whether obj is a valid file handle.
Returns the string "file" if obj is an open file handle,
"closed file" if obj is a closed file handle,
or fail if obj is not a file handle.
io.popen (prog, mode):在另外的程序中打开文件,返回文件句柄
# io.popen (prog [, mode]):在另外的程序中打开文件,返回文件句柄
该函数依赖于系统,部分平台不支持
This function is system dependent and is not available on all platforms.
Starts the program prog in a separated process and returns a file handle
that you can use to read data from this program (if mode is "r", the default)
or to write data to this program (if mode is "w").
使用示例
示例:io.read
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 以默认模式(只读模式)打开文件
> file=io.open("file")
-- 设置默认输入源为file(默认是控制台)
> io.input(file)
file (0x7ff85cfd4880)
-- 读取第一行
> io.read()
瓜田李下
-- 读取第二行
> io.read()
瓜田李下2
-- 读取第三行(保留文件分隔符),多出一个行
> io.read("L")
瓜田李下3
-- 读取第四行(保留文件分隔符),多出一个行
> io.read("*L")
瓜田李下4
-- 继续读取,没有文件内容,输出nil
> io.read("L")
nil
示例:io.read("n")
huli@hudeMacBook-Pro ~ % cat file4
2
323
12131
2322
huli@hudeMacBook-Pro ~ % lua
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
> file=io.open("file4")
> io.input(file)
file (0x7ff8462c8880)
-- 读取连续的数字行数据
> io.read("n")
2
> io.read("n")
323
> io.read("n")
12131
> io.read("n")
2322
> io.read("n")
nil
> io.read("n")
nil
示例:io.read("n")
huli@hudeMacBook-Pro ~ % cat file4
2
323
瓜田李下
2322
huli@hudeMacBook-Pro ~ % lua
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
> file=io.open("file4")
> io.input(file)
file (0x7ff8462c8880)
-- 读取连续的数字行数据,如不连续则终止,或者读到最后一行
> io.read("n")
2
> io.read("n")
323
> io.read("n")
nil
> io.read("n")
nil
> io.read("n")
nil
> io.read("n")
nil
示例:io.write
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 打开文件
> file=io.open("file","w")
-- 设置输出源为file
> io.output(file)
file (0x7ff85cfd4880)
-- 写入文件
> io.write("瓜田李下 gtlx")
file (0x7ff85cfd4880)
-- 需执行flush操作,否则不会输出到文件
> io.flush()
true
-- 退出后,查看文件内容
huli@hudeMacBook-Pro ~ % cat file
瓜田李下 gtlx%
示例:io.lines(filename)
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 不指定文件,默认从控制台读取数据
> io.lines()
function: 0x600002d4c000
> for value in io.lines() do print(value) end
hello
hello
hello2
hello2
-- 指定读取的文件
> for value in io.lines("file") do print(value) end
瓜田李下
瓜田李下2
瓜田李下3
瓜田李下4
瓜田李下5
瓜田李下6