| 
Lua 常用库系列:第十四篇【文件系统库】 文件系统操作是应用程序开发中的常见需求,包括文件的读取、写入、删除等操作。Lua 提供了多种库用于文件系统操作,常见的包括标准库 io、lfs(LuaFileSystem)等。本文将详细介绍 Lua 的文件系统库及其常用函数和使用方法。 文件系统库概述Lua 的标准库 io 提供了基本的文件操作功能,而 lfs(LuaFileSystem)则提供了更强大的文件系统操作功能,如目录操作、文件属性获取等。本文将主要介绍如何使用 io 和 lfs 进行文件系统操作。 一、io 库的基本使用1. 打开和关闭文件 local file = io.open("example.txt", "r")  -- 以只读方式打开文件 
if file then 
    -- 读取文件内容 
    local content = file:read("*a") 
    print(content) 
    -- 关闭文件 
    file:close() 
else 
    print("无法打开文件") 
end2. 读取文件 local file = io.open("example.txt", "r") 
if file then 
    -- 按行读取 
    for line in file:lines() do 
        print(line) 
    end 
    file:close() 
else 
    print("无法打开文件") 
end3. 写入文件 local file = io.open("example.txt", "w")  -- 以写入方式打开文件 
if file then 
    file:write("Hello, Lua!\n") 
    file:write("这是第二行文本\n") 
    file:close() 
else 
    print("无法打开文件") 
end4. 追加写入文件 local file = io.open("example.txt", "a")  -- 以追加方式打开文件 
if file then 
    file:write("这是追加的文本\n") 
    file:close() 
else 
    print("无法打开文件") 
end二、LuaFileSystem(lfs)库的使用lfs 库提供了更强大的文件系统操作功能,如目录操作、文件属性获取等。 安装 LuaFileSystem 可以使用 LuaRocks 安装 lfs: luarocks install luafilesystem1. 获取文件属性 local lfs = require("lfs") 
 
local attr = lfs.attributes("example.txt") 
if attr then 
    for key, value in pairs(attr) do 
        print(key, value) 
    end 
else 
    print("无法获取文件属性") 
end2. 遍历目录 local lfs = require("lfs") 
 
for file in lfs.dir(".") do 
    print(file) 
end3. 创建和删除目录 local lfs = require("lfs") 
 
-- 创建目录 
local success, err = lfs.mkdir("new_directory") 
if success then 
    print("目录创建成功") 
else 
    print("目录创建失败: " .. err) 
end 
 
-- 删除目录 
local success, err = lfs.rmdir("new_directory") 
if success then 
    print("目录删除成功") 
else 
    print("目录删除失败: " .. err) 
end4. 更改当前工作目录 local lfs = require("lfs") 
 
local current_dir = lfs.currentdir() 
print("当前工作目录: " .. current_dir) 
 
local success, err = lfs.chdir("new_directory") 
if success then 
    print("工作目录更改成功") 
else 
    print("工作目录更改失败: " .. err) 
end 
 
print("新工作目录: " .. lfs.currentdir())三、实用小工具在文件系统操作中,一些实用的小工具函数可以提高开发效率。以下是两个常用的小工具函数示例: function fileExists(filename) 
    local file = io.open(filename, "r") 
    if file then 
        file:close() 
        return true 
    else 
        return false 
    end 
end 
 
print(fileExists("example.txt"))  -- 输出: true 或 falsefunction readFileToTable(filename) 
    local lines = {} 
    local file = io.open(filename, "r") 
    if file then 
        for line in file:lines() do 
            table.insert(lines, line) 
        end 
        file:close() 
    else 
        print("无法打开文件") 
    end 
    return lines 
end 
 
local lines = readFileToTable("example.txt") 
for i, line in ipairs(lines) do 
    print(i, line) 
end四、综合示例通过组合使用 io 和 lfs 库,可以实现一个简单的文件管理工具。以下示例演示如何列出目录中的所有文件,并显示每个文件的大小: file_manager.lua local lfs = require("lfs") 
 
function listFiles(directory) 
    for file in lfs.dir(directory) do 
        if file ~= "." and file ~= ".." then 
            local filePath = directory .. "/" .. file 
            local attr = lfs.attributes(filePath) 
            if attr.mode == "file" then 
                print(string.format("文件: %s, 大小: %d 字节", file, attr.size)) 
            elseif attr.mode == "directory" then 
                print("目录: " .. file) 
            end 
        end 
    end 
end 
 
listFiles(".")将以上代码保存为 file_manager.lua,然后使用 Lua 运行: lua file_manager.lua总结本文介绍了 Lua 文件系统库中的 io 和 lfs 库的常用函数及其使用方法。这些函数提供了强大的文件系统操作能力,帮助开发者高效地进行文件和目录的管理。  |