Lua file I/O
Lua I/O library is used to read and process files. It is divided into simple mode (same as C) and complete mode.
The simple model has a current input file and a current output file, and provides operations related to these files.
Complete model (complete model) is implemented using external file handles. It defines all file operations as file handle methods in an object-oriented form
Simple mode is more appropriate when doing some simple file operations. But when performing some advanced file operations, the simple mode seems to be inadequate. For example, for operations such as reading multiple files at the same time, it is more appropriate to use the full mode.
The open file operation statement is as follows:
file = io.open (filename [, mode])
The values of mode are:
Mode | Description |
---|---|
r | Open the file in read-only mode. The file must exist. |
w | Open a write-only file. If the file exists, the file length will be cleared to 0, that is, the file content will disappear. If the file does not exist, create the file. |
a | Open a write-only file in append mode. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (EOF characters are reserved) |
r+ | Open the file in read-write mode. The file must exist. |
w+ | Open a readable and writable file. If the file exists, the file length will be cleared to zero, that is, the file content will disappear. If the file does not exist, create the file. |
a+ | Similar to a, but this file is readable and writable |
b | Binary mode , if the file is a binary file, you can add b |
+ | to indicate that the file can be both read and written |
Simple mode
Simple mode uses standard I/O or uses one current input file and one current output file.
The following is the file.lua file code. The file to be operated is test.lua (if it does not exist, you need to create this file). The code is as follows:
-- 以只读方式打开文件 file = io.open("test.lua", "r") -- 设置默认输入文件为 test.lua io.input(file) -- 输出文件第一行 print(io.read()) -- 关闭打开的文件 io.close(file) -- 以附加的方式打开只写文件 file = io.open("test.lua", "a") -- 设置默认输出文件为 test.lua io.output(file) -- 在文件最后一行添加 Lua 注释 io.write("-- test.lua 文件末尾注释") -- 关闭打开的文件 io.close(file)
Execute the above code, you will find that the output The first line of information in the test.ua file is changed, and the lua comment is added to the last line of the file. For example, the output here is:
-- test.lua 文件
In the above example, we used the io."x" method, in which we did not take parameters in io.read(). The parameters can be one of the following tables:
##"*l" (default) Read the next line and return nil at the end of the file (EOF) . Example: file.read("*l")numberReturns a string with the specified number of characters, or returns nil at EOF. Example: file.read(5)Other io methods are:
io.tmpfile():Returns a temporary file handle. The file is opened in update mode and automatically Delete
io.type(file): Check if obj is an available file handle
io .flush(): Writes all the data in the buffer to the file
io.lines(optional file name): Returns an iterative function, each This call will obtain a line of content in the file. When the end of the file is reached, nil will be returned, but the file will not be closed.
Full mode
Usually we need Process multiple files at the same time. We need to use file:function_name instead of io.function_name method. The following example demonstrates how to process the same file at the same time:
-- 以只读方式打开文件 file = io.open("test.lua", "r") -- 输出文件第一行 print(file:read()) -- 关闭打开的文件 file:close() -- 以附加的方式打开只写文件 file = io.open("test.lua", "a") -- 在文件最后一行添加 Lua 注释 file:write("--test") -- 关闭打开的文件 file:close()
Execute the above code, you will find that the first line of information of the test.ua file is output, and the lua comment is added to the last line of the file. For example, the output here is:
-- test.lua 文件
read parameters are consistent with the simple mode.
Other methods:
##file:seek(optional whence, optional offset): Set and get the current file position, and return the final if successful File location (in bytes), returns nil plus error message if failed. The value of the parameter whence can be:
Without the parameter file:seek() returns the current position, file:seek("set") locates the file header, file:seek("end") locates the The end of the file and returns the file size- "set": Start from the beginning of the file
- "cur": Start from the current position[ Default]
- "end": Start from the end of the file
- offset: The default is 0
file:flush(): Write all the data in the buffer to the file
io.lines(optional file name): Open the specified file filename in read mode and return an iterative function. Each call will obtain a line of content in the file. When the end of the file is reached, nil will be returned and the file will be automatically closed. Without parameters, io.lines() <=> io.input():lines(); reads the contents of the default input device, but does not close the file at the end, such as
for line in io.lines("main.lua") do print(line) end
-- 以只读方式打开文件 file = io.open("test.lua", "r") file:seek("end",-25) print(file:read("*a")) -- 关闭打开的文件 file:close()The output result from my side is:
st.lua 文件末尾--test
Pattern | Description |
---|---|
"*n" | Read a number and Return it. Example: file.read("*n") |
"*a" | Read the entire file from the current position. Example: file.read("*a") |