Easy Tutorial
❮ Lua Break Statement Lua Tutorial ❯

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 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:


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:

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
❮ Lua Break Statement Lua Tutorial ❯