The io operation module extends lua's built-in io module to provide more easy-to-use interfaces.
This is a native interface for lua. For detailed usage, see Lua's official documentation: The Complete I/O Model
If you want to read all the contents of the file, you can write:
local file = io.open("$(tmpdir)/file.txt", "r")
if file then
local data = file:read("*all")
file:close()
end
Or you can read it more quickly using io.readfile.
If you want to write a file, you can do this:
-- Open file: w is write mode, a is append write mode
local file = io.open("xxx.txt", "w")
if file then
-- Write data to file with native lua interface, does not support formatting, no line breaks, does not support built-in variables
file:write("hello xmake\n")
-- Write data to file with xmake extended interface, support formatting, no line breaks, no built-in variables
file:writef("hello %s\n", "xmake")
-- Use xmake extended formatted parameters to write to one line, with line breaks, and support for built-in variables
file:print("hello %s and $(buildir)", "xmake")
-- Write a line using the xmake extended formatted arguments, no line breaks, and support for built-in variables
file:printf("hello %s and $(buildir) \n", "xmake")
-- Close the file
file:close()
end
You can load serialized table contents from a file, generally used with io.save, for example:
-- Load the contents of the serialized file to the table
local data = io.load("xxx.txt")
if data then
-- Dump prints the contents of the entire table in the terminal, formatting the output
utils.dump(data)
end
You can serialize the contents of the table to the specified file, generally used in conjunction with io.load, for example:
io.save("xxx.txt", {a = "a", b = "b", c = "c"})
The result of the storage is:
{
["b"] = "b"
, ["a"] = "a"
, ["c"] = "c"
}
It is more convenient to directly read the contents of the entire file without opening the file, for example:
local data = io.readfile("xxx.txt")
It is more convenient to directly write the contents of the entire file without opening the file, for example:
io.writefile("xxx.txt", "all data")
Similar to the string.gsub interface, the full-text pattern matches the replacement content, but here is the direct operation file, for example:
-- Remove all whitespace characters from the file
io.gsub("xxx.txt", "%s+", "")
Reads the data of the specified number of lines at the end of the file and displays a command like cat xxx.txt | tail -n 10, for example:
-- Display the last 10 lines of the file
io.tail("xxx.txt", 10)
Read all the contents of the file and display it, similar to the cat xxx.txt command, for example:
io.cat("xxx.txt")
Directly format the passed parameter to output a line of string to the file with a line break, for example:
io.print("xxx.txt", "hello %s!", "xmake")
Directly format the passed parameter to output a line of string to the file without a line break, for example:
io.printf("xxx.txt", "hello %s!\n", "xmake")
Returns all lines from a given file name
local lines = io.lines("xxx.txt")
for line in lines do
print(line)
end
Returns a file for a given std file name
-- returns stdin
io.stdin
-- returns stdout
io.stdout
-- returns stderr
io.stderr
Returns a file lock object when successfully locking the file
local lock = io.openlock("xxx.txt")
lock:lock()
lock:unlock()
lock:close()
Replaces a given pattern in a file by a replacement string
-- replace string "Hello" in "xxx.txt" with "World"
io.replace("xxx.txt", "Hello", "World")
-- if you want to replace a string and not a pattern
io.replace("xxx.txt", "1+1=2", "2+2=4", {plain = true})