English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Lua I/The O library is used for reading and processing files. It is divided into simple mode (like C) and complete mode.
Simple mode (simple model) has a current input file and a current output file, and provides operations related to these files.
Complete mode (complete model) uses external file handles to implement. It defines all file operations as methods of the file handle in an object-oriented manner
Simple mode is more suitable for some simple file operations. However, when performing some advanced file operations, simple mode may not be enough. For example, it is more suitable to use complete mode for operations such as reading multiple files at the same time.
The statement to open a file is as follows:
file = io.open (filename[, mode])
The value of mode includes:
Mode | Description |
---|---|
r | Open a file in read-only mode, the file must exist. |
w | Open a write-only file, if the file exists, the file length is cleared to 0, that is, the content of the file will be deleted. If the file does not exist, the file will be created. |
a | Open a write-only file in append mode. If the file does not exist, it will be created. If the file exists, the data to be written will be appended to the end of the file, that is, the original content of the file will be retained. (EOF symbol is retained) |
r+ | Open a file in read and write mode, the file must exist. |
w+ | Open a file in read and write mode, if the file exists, the file length is cleared to zero, that is, the content of the file will be deleted. If the file does not exist, the file will be created. |
a+ | Similar to a, but this file can be read and written |
b | Binary mode, if the file is a binary file, you can add b |
+ | number indicates that the file can be read and written |
Simple mode uses the standard I/Or use a current input file and a current output file.
The following is the code of file.lua file, the file being operated is test.lua (if you need to create the file, do so). The code is as follows:
-- Open the file in read-only mode file = io.open("test.lua", "r") -- Set the default input file to test.lua io.input(file) -- Output the first line of the file print(io.read()) -- Close the opened file io.close(file) -- Open a write-only file in append mode file = io.open("test.lua", "a") -- Set the default output file to test.lua io.output(file) -- Add Lua comments at the end of the file io.write("-- test.lua file end comment") -- Close the opened file io.close(file)
After executing the above code, you will find that the first line of information from the test.lua file is output, and Lua comments are added at the end of the file. As for my output, it is as follows:
-- test.lua file
In the above examples, we used the io."x" method, where we did not include parameters in io.read(). The parameters can be one of the following from the table below:
Mode | Description |
---|---|
"*n" | Read a number and return it. For example, file.read("*n") |
"*a" | Read the entire file from the current position. For example, file.read("*a") |
"*l" (default) | Read the next line and return nil at the end of the file (EOF). For example, file.read("*l") |
number | Returns a string of a specified number of characters, or nil at EOF. For example, file.read("5) |
Other io methods include:
io.tmpfile():Returns a temporary file handle that is opened in update mode and is automatically deleted when the program ends
io.type(file): Check if obj is a usable file handle
io.flush(): Write all the data in the file buffer
io.lines(optional file name): Returns an iterator function, which will obtain a line of content from the file each time it is called. When it reaches the end of the file, it will return nil but not close the file
Generally, we need to 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 simultaneously:
-- Open the file in read-only mode file = io.open("test.lua", "r") -- Output the first line of the file print(file:read()) -- Close the opened file file:close() -- Open a write-only file in append mode file = io.open("test.lua", "a") -- Add Lua comments at the end of the file file:write("--test) -- Close the opened file file:close()
After executing the above code, you will find that the first line of information from the test.ua file is output, and Lua comments are added at the end of the file. As for my output, it is as follows:
-- test.lua file
The parameters of 'read' are consistent with the simple mode.
Other methods:
file:seek(optional whence, optional offset): Set and get the current file position, return the final file position (in bytes) if successful, return nil plus error information if failed. The value of the whence parameter can be:
Without parameters, file:seek() returns the current position, file:seek("set") locates to the beginning of the file, file:seek("end") locates to 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: default is 0
file:flush(): Write all the data in the file buffer
io.lines(optional file name): Open the specified file filename in read mode and return an iteration function, which will obtain a line of content from the file each time it is called. When it reaches the end of the file, it will return nil and close the file automatically.
If io.lines() is called without parameters, io.input():lines(); reads the content of the default input device, but the file is not closed at the end, such as:
for line in io.lines("main.lua") do print(line) end
The following example uses the seek method to locate the last 25 position and uses the read method of *a parameter, which is from the current position (the last 25 position) reads the entire file.
-- Open the file in read-only mode file = io.open("test.lua", "r") file:seek("end",-25) print(file:read("*a")) -- Close the opened file file:close()
The result I output here is:
st.lua End of File--test