• lua的文件管理


    lua没有自己的文件管理 只有读取和写入文件,但是可以通过调用lfs(LuaFileSystem),lfs是一个

    用于lua进行文件访问的库,支持lua5.1和lua5.2,并且跨平台

    lfs的使用:

    "lfs" = {  --dump(lfs )
      "_COPYRIGHT"      = "Copyright (C) 2003 Kepler Project"
      "_DESCRIPTION"   = "LuaFileSystem is a Lua library developed to complement the set of functions   related to file systems offered by the standard Lua distribution"
      "_VERSION"      = "LuaFileSystem 1.4.2"
      "attributes"         = function: 00B3D7A8
      "chdir"                = function: 00B3D7C8
      "currentdir"      = function: 00B3D7E8
      "dir"         = function: 00B3D808
      "lock"         = function: 00B3D828
      "mkdir"         = function: 00B3D868
      "rmdir"       = function: 00B3D888
      "setmode"              = function: 00B3D8C8
      "symlinkattributes" = function: 00B3D8A8
      "touch"        = function: 00B3D908
      "unlock"      = function: 00B3D948
    }

    常用的方法:

    lfs.currentdir() --返回当前所在的全路径字符串

    lfs.attributes(dir) -- 返回文件的属性table

    lfs.dir(path)--用于遍历文件加中的对象

      

    --遍历
    function getAllFiles(path, files)
        files = files or {}
        for file in lfs.dir(path) do
            if file ~= "." and file ~= ".." then
                local subPath = path .. "\" .. file 
                local attr = lfs.attributes(subPath)
                assert(type(attr) == "table")
                if attr.mode == "directory" then
                    getAllFiles(subPath, files)
                else
                    table.insert(files, subPath)                                              
                end 
            end
        end
        return files
    end
    
    --查找
    function findInDir (path, wefind, r_table, intofolder)  
        for file in lfs.dir(path) do  
            if file ~= "." and file ~= ".." then  
                print(file)
                local f = path..'/'..file  
                if string.find(f, wefind) ~= nil then  
                    table.insert(r_table, f)  
                end  
                local attr = lfs.attributes(f)  
                assert(type(attr) == "table")  
                if attr.mode == "directory" and intofolder then  
                    findInDir(f, wefind, r_table, intofolder)  
                else  
    
                end  
            end  
        end  
    end
  • 相关阅读:
    Element-ui 的 slot 关系理解
    关于Delegate委托和Event事件的学习
    JavaScript 中 prototype 与 __proto__
    正向代理与反向代理的个人理解
    MVC和三层架构
    关于SqlDataAdapter的思考
    关于C#连接Oracle数据库
    关于VS配置环境
    富文本的实现
    博客
  • 原文地址:https://www.cnblogs.com/Caesar7/p/7550766.html
Copyright © 2020-2023  润新知