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:
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") |
Read the next line and return nil at the end of the file (EOF) . Example: file.read("*l") | |
Returns a string with the specified number of characters, or returns nil at EOF. Example: file.read(5) |