Skip to content

文件 I/O

文件 I/O 是 Lua 中用于读写文件的操作。本章节将介绍 Lua 中文件 I/O 的基本操作和常用函数。

文件操作模式

在 Lua 中,打开文件时需要指定操作模式:

模式描述
r只读模式,文件指针在文件开头
w只写模式,创建新文件或截断现有文件
a追加模式,文件指针在文件末尾
r+读写模式,文件指针在文件开头
w+读写模式,创建新文件或截断现有文件
a+读写模式,文件指针在文件末尾

文件的打开和关闭

打开文件

使用 io.open 函数打开文件:

lua
local file = io.open("test.txt", "r")
if not file then
  print("文件打开失败")
  return
end

关闭文件

使用 file:close() 方法关闭文件:

lua
local file = io.open("test.txt", "r")
if file then
  -- 文件操作
  file:close()
end

文件的读取

读取整个文件

lua
local file = io.open("test.txt", "r")
if file then
  local content = file:read("*")
  print(content)
  file:close()
end

按行读取

lua
local file = io.open("test.txt", "r")
if file then
  for line in file:lines() do
    print(line)
  end
  file:close()
end

读取指定字符数

lua
local file = io.open("test.txt", "r")
if file then
  local content = file:read(10)  -- 读取 10 个字符
  print(content)
  file:close()
end

读取一个单词

lua
local file = io.open("test.txt", "r")
if file then
  local word = file:read("*w")  -- 读取一个单词
  print(word)
  file:close()
end

文件的写入

写入字符串

lua
local file = io.open("test.txt", "w")
if file then
  file:write("Hello, Lua!")
  file:close()
end

写入多行

lua
local file = io.open("test.txt", "w")
if file then
  file:write("第一行\n")
  file:write("第二行\n")
  file:write("第三行\n")
  file:close()
end

格式化写入

lua
local file = io.open("test.txt", "w")
if file then
  local name = "张三"
  local age = 30
  file:write(string.format("姓名:%s,年龄:%d\n", name, age))
  file:close()
end

文件指针操作

获取文件指针位置

lua
local file = io.open("test.txt", "r")
if file then
  local pos = file:seek()  -- 获取当前文件指针位置
  print("当前位置:" .. pos)
  file:close()
end

设置文件指针位置

lua
local file = io.open("test.txt", "r")
if file then
  file:seek("set", 10)  -- 设置文件指针到文件开头后 10 个字节的位置
  local content = file:read("*")
  print(content)
  file:close()
end

seek 方法的参数:

  • "set":从文件开头开始计算
  • "cur":从当前位置开始计算
  • "end":从文件末尾开始计算

标准文件

Lua 提供了三个标准文件:

  • io.stdin:标准输入
  • io.stdout:标准输出
  • io.stderr:标准错误

读取标准输入

lua
print("请输入你的名字:")
local name = io.read()
print("你好," .. name .. "!")

写入标准输出

lua
io.write("Hello, Lua!\n")

写入标准错误

lua
io.stderr:write("错误信息\n")

文件操作的应用

示例 1:复制文件

lua
function copyFile(src, dst)
  local srcFile = io.open(src, "r")
  if not srcFile then
    print("源文件打开失败")
    return false
  end
  
  local dstFile = io.open(dst, "w")
  if not dstFile then
    print("目标文件打开失败")
    srcFile:close()
    return false
  end
  
  local content = srcFile:read("*")
  dstFile:write(content)
  
  srcFile:close()
  dstFile:close()
  return true
end

copyFile("source.txt", "destination.txt")

示例 2:统计文件行数

lua
function countLines(filename)
  local file = io.open(filename, "r")
  if not file then
    print("文件打开失败")
    return 0
  end
  
  local count = 0
  for line in file:lines() do
    count = count + 1
  end
  
  file:close()
  return count
end

local lines = countLines("test.txt")
print("文件行数:" .. lines)

示例 3:读取 CSV 文件

lua
function readCSV(filename)
  local file = io.open(filename, "r")
  if not file then
    print("文件打开失败")
    return {}
  end
  
  local data = {}
  for line in file:lines() do
    local row = {}
    for value in line:gmatch("([^",]+)") do
      table.insert(row, value)
    end
    table.insert(data, row)
  end
  
  file:close()
  return data
end

local csvData = readCSV("data.csv")
for i, row in ipairs(csvData) do
  for j, value in ipairs(row) do
    io.write(value, "\t")
  end
  io.write("\n")
end

小结

本章节介绍了 Lua 中文件 I/O 的基本操作,包括文件的打开和关闭、读取和写入、文件指针操作,以及标准文件的使用。掌握这些内容,对于编写 Lua 程序非常重要。