Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Lua File I/O

Lua File I/O

The Lua I/O library is used to read and process files. It is divided into simple mode (like C) and complete mode.

Simple mode is more suitable for simple file operations. However, when performing some advanced file operations, simple mode becomes inadequate. For example, for operations like reading multiple files simultaneously, complete mode is more suitable.

The statement for opening a file 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, its length is cleared to 0, meaning its contents will disappear. If the file does not exist, the file is 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 written data will be added to the end of the file, meaning the original contents of the file will be preserved. (EOF character preserved)
r+ Open the file in read-write mode; the file must exist.
w+ Open a read-write file. If the file exists, its length is cleared to zero, meaning its contents will disappear. If the file does not exist, the file is created.
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
+ indicates that the file can be both read and written

Simple mode

Simple mode uses standard I/O or a current input file and a current output file.

The following is the code of the file.lua file. The file being operated on is test.lua (if you don't have it, you need to create this file). The code is as follows:

Example

-- 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 a Lua comment at the last line of the file

io.write

(

"-- comment at the end of test.lua file"

)

-- Close the opened file

io.close

(

file

)

After executing the above code, you will find that the first line of the test.lua file is output, and a lua comment is added at the last line of the file. For example, what I output is:



-- test.lua file

In the above example we used the io."x" method. In io.read() we didn't pass any arguments. The argument can be one of the following:

Mode 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")
"*l" (default) Read the next line, returning nil at end of file (EOF). Example: file.read("*l")
number Return a string of a specified number of characters, or nil at EOF. Example: file.read(5)

Other io methods include:


Complete mode

Usually we need to process multiple files at the same time. We need to use file:function_name instead of the io.function_name method. The following example demonstrates how to process the same file at the same time:

Example

-- 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 a Lua comment at the last line 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 the test.lua file is output, and a lua comment is added at the last line of the file. For example, what I output is:



-- test.lua file

The parameters of read are the same as in simple mode.

Other methods:

Calling file:seek() without arguments returns the current position, file:seek("set") positions to the beginning of the file, and file:seek("end") positions 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: defaults to 0
io.lines() when called without arguments <=> io.input():lines(); reads the contents of the default input device, but does not close the file when finished, for example:

for line in io.lines("main.lua") do

  print(line)

  end

The following example uses the seek method to locate the 25th position from the end of the file and uses the *a argument of the read method, that is, reads the entire file from the current position (the 25th position from the end).

Example

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



st.lua 文件末尾--test