Lua File I/O
The Lua I/O library is used for reading and processing files. It is divided into simple mode (similar to 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. It defines all file operations as methods of the file handle in an object-oriented manner.
Simple mode is suitable for simple file operations. However, when performing advanced file operations, such as reading multiple files simultaneously, the complete mode is more appropriate.
The syntax for opening a file is as follows:
file = io.open(filename [, mode])
The values for mode
are:
Mode | Description |
---|---|
r | Opens a file for reading. The file must exist. |
w | Opens a file for writing. If the file exists, its length is truncated to 0, erasing its content. If the file does not exist, it is created. |
a | Opens a file for appending. If the file does not exist, it is created. If the file exists, data is written to the end of the file, preserving the original content. (EOF character is preserved) |
r+ | Opens a file for both reading and writing. The file must exist. |
w+ | Opens a file for both reading and writing. If the file exists, its length is truncated to 0, erasing its content. If the file does not exist, it is created. |
a+ | Similar to 'a', but the file can also be read. |
b | Binary mode. If the file is a binary file, 'b' can be added. |
+ | Indicates that the file can be read from and written to. |
Simple Mode
Simple mode uses standard I/O or a current input file and a current output file.
The following is the code for the file.lua
file, which operates on test.lua
(if it does not exist, you need to create it). The code is as follows:
Example
-- Open the file in read 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 the file in append mode
file = io.open("test.lua", "a")
-- Set the default output file to test.lua
io.output(file)
-- Add a Lua comment at the end of the file
io.write("-- test.lua file end comment")
-- Close the opened file
io.close(file)
Executing the above code will output the first line of the test.lua
file and add a Lua comment at the end of the file. For example, the output might be:
-- test.lua file
In the above example, we used the io."x"
methods. In io.read()
, we did not provide a parameter. The parameter can be one of the following:
Mode | Description |
---|---|
"*n" | Reads a number and returns it. Example: file.read("*n") |
"*a" | Reads the entire file from the current position. Example: file.read("*a") |
"*l" (default) | Reads the next line, returning nil at the end of the file (EOF). Example: file.read("*l") |
number | Returns a string with the specified number of characters, or nil at EOF. Example: file.read(5) |
Other io
methods include:
io.tmpfile(): Returns a temporary file handle, which is opened in update mode and is automatically deleted at the end of the program.
io.type(file): Checks if
obj
is a usable file handle.io.flush(): Writes all buffered data to the file.
io.lines(optional file name): Returns an iterator function that, each time it is called, gets a line from the file. It returns
nil
at the end of the file but does not close the file.
Complete Mode
Often, we need to handle multiple files at the same time. We use file:function_name
instead of io.function_name
. The following example demonstrates how to handle the same file simultaneously:
Example
-- Open the file in read mode
file = io.open("test.lua", "r")
-- Output the first line of the file
print(file:read())
-- Close the opened file
file:close()
-- Open the file in append mode
file = io.open("test.lua", "a")
-- Add a Lua comment at the end of the file
file:write("--test")
-- Close the opened file
file:close()
Executing the above code will output the first line of the test.lua
file and add a Lua comment at the end of the file. For example, the output might be:
-- test.lua file
The parameters for read
are the same as in simple mode.
Other methods include:
file:seek(optional whence, optional offset): Sets and gets the current file position. It returns the final file position (in bytes) if successful, or
nil
plus an error message if unsuccessful. Thewhence
values can be:- "set": From the beginning of the file.
- "cur": From the current position (default).
- "end": From the end of the file.
offset
: Defaults to 0.
file:flush(): Writes all buffered data to the file.
io.lines(optional file name): Opens the specified file
filename
in read mode and returns an iterator function. Each call gets a line from the file. It returnsnil
at the end of the file and automatically closes the file.for line in io.lines("main.lua") do print(line) end
The following example uses the seek
method to position at the 25th byte from the end of the file and uses the *a
parameter of the read
method to read the entire file from the current position (25th byte from the end).
Example
-- Open the file in read mode
file = io.open("test.lua", "r")
file:seek("end", -25)
print(file:read("*a"))
-- Close the opened file
file:close()
The output might be:
st.lua file end--test