鸿蒙安全合规:Flutter混合应用中的数据加密与权限管控实战
2025/12/17 6:33:51
Lua 的 IO (输入/输出)模块 用于处理文件输入输出操作,主要通过io库实现。这个模块提供了简单而强大的文件操作功能,使得 Lua 能够高效地读写文件数据。
io.open(filename [, mode]):以指定模式打开文件local file = io.open("test.txt", "r")file:close():关闭已打开的文件file:close()file:read(format):从文件中读取数据local line = file:read("*l")file:write(value):向文件写入数据file:write("Hello Lua!\n")io.read():从标准输入读取io.write():向标准输出写入io.input()/io.output():设置默认输入/输出文件file:seek([whence][, offset]):移动文件指针file:seek("end", -10)移动到文件倒数第10字节处file:flush():立即将缓冲区内容写入文件io.lines([filename]):返回文件行的迭代器Lua 可以通过组合使用os.tmpname()和io.open()来创建和处理临时文件
通过添加 “b” 模式标志来读写二进制文件
local binfile = io.open("data.bin", "rb")-- 读取文件内容localfile=io.open("data.txt","r")iffilethenlocalcontent=file:read("*a")file:close()print(content)end-- 写入文件localout=io.open("output.txt","w")ifoutthenout:write("This is line 1\n")out:write("This is line 2\n")out:close()end-- 逐行处理文件forlineinio.lines("bigfile.txt")do-- 处理每一行print(#line)-- 打印每行长度endLua 的 IO 模块虽然简单,但功能齐全,足以满足大多数文件操作需求。对于更高级的文件系统操作,可以结合使用os模块提供的功能。