The Lua I/O library is used to read and process files. It is divided into simple mode (like C) and full mode.
-
The simple mode (simple model) has a current input file and a current outputfile, and provides operations related to these files.
-
Full mode (complete model) is implemented using external file handles. It defines all file operations as file handles in the form of facing objects.
Simple mode is more appropriate when doing some simple file operations. However, when doing some advanced file operations, the simple mode seems inadequate. For example, for operations such as reading multiple files at the same time, full mode is more appropriate.
The operation statement to open the file is as follows:
file = io.open (filename [, mode])
The
mode
values are:
|
Pattern |
Description |
|---|---|
|
|
Open the file as read-only. The file must exist. |
|
|
Open a write-only file, and if the file exists, the length of the file is cleared to 0, that is, the contents of the file will disappear. Create a file if it does not exist. |
|
|
Open the write-only file as an attachment. If the file does not exist, the file is created, and if the file exists, the written data is added to the end of the file, that is, the original contents of the file are retained. (EOF character reserved) |
|
|
Open the file in a read-write manner. The file must exist. |
|
|
Open a read-write file, and if the file exists, the length of the file is cleared to zero, that is, the contents of the file will disappear. Create a file if it does not exist. |
|
|
Similar to a, but this file is readable and writable |
|
|
Binary mode, if the file is binary, you can add b |
|
|
Number indicates that the file can be read or written. |
4.26.1. Simple mode #
The simple mode uses the standard I/O or uses a current input file anda current output file.
The following is
file.lua
file code, the file for the operation is
test.lua
(if you don’t need to create the file), the code is as follows:
Example #
-- Open file as read-only
file = io.open("test.lua", "r")
-- Set the default input file to test.lua
io.input(file)
-- Output file first line
print(io.read())
-- Close Open Files
io.close(file)
-- Open write only files as attachments
file = io.open("test.lua", "a")
-- Set the default output file to test.lua
io.output(file)
-- Add Lua comments on the last line of the file
io.write("-- test.lua Comment at the end of the file")
-- Close Open Files
io.close(file)
Execute the above code, and you will find that the output
test.lua
the first line of information in the file, and added to the last line of the file
lua
the comments of. For example, the output from my side is:
-- test.lua file
In the above example, we used the
io."x"
method, where
io.read()
we do not have a parameter, which can be one of the following tables:
|
Pattern |
Description |
|---|---|
|
|
Read a number and return it. Example:
|
|
|
Reads the entire file from the current location. Example:
|
|
|
Read the next line and return nil at the end of the file (EOF). Example:
|
|
|
Returns a string with a specified number of characters, or nil on EOF. Example:
|
Others
io
the methods are:
-
io.tmpfile()returns a temporary file handle that opens in update mode and is automatically deleted at the end of the program -
io.type(file)detectionobjwhether it is an available file handle -
io.flush()write all data in the buffer to the file -
io.lines(optional file name)returns an iterative function, and each call will get a line in the file, and when it comes to the end of the file, it will returnnilbut do not close the file
4.26.2. Complete mode #
Usually 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 shows how to process the same file at the same time:
Example #
-- Open file as read-only
file = io.open("test.lua", "r")
-- Output file first line
print(file:read())
-- Close Open Files
file:close()
-- Open write only files as attachments
file = io.open("test.lua", "a")
-- Add Lua comments on the last line of the file
file:write("--test")
-- Close Open Files
file:close()
Execute the above code, and you will find that the output
test.lua
the first line of information in the file, and added to the last line of the file
lua
the comments of. For example, the output from my side is:
-- test.lua file
read
is consistent with the simple mode
Other methods:
-
file:seek(optional whence, optional offset)set and get the current file location, return the final file location (in bytes) if you succeed, or return it if you failniladd an error message. Parameters. `` whence``the value can be:-
"set": start from the header of the file -
"cur": start from the current position [default] -
"end": Start at the end of the file -
offset: default is 0
Without parameters
file:seek()then return to the current positionfile:seek("set")navigate to the file headerfile:seek("end")navigate to the end of the file and return the file size -
-
file:flush(): write all data in the buffer to the file -
io.lines(optional file name)open the specified filefilenameto read the mode and return an iterative function, each call will get a line inthe file, and when it comes to the end of the file, it will return `` nil``and automatically close the file.If there are no parametersio.lines() <=> io.input():lines()read the contents of the default input device, but do not close the file at the end, such as:
for line in io.lines("main.lua") do
print(line)
end
The following example uses the
seek
method, navigate to the penultimatelocation of the file and use the
read
method of
*a
parameter, that is, the entire file is read from the current location (the penultimate 25th location).
Example #
-- Open file as read-only
file = io.open("test.lua", "r")
file:seek("end",-25)
print(file:read("*a"))
-- Close Open Files
file:close()
The output from my side is:
st.lua end of file--test