zoukankan      html  css  js  c++  java
  • LuaFileSystem

    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
  • 相关阅读:
    Max History CodeForces
    Buy a Ticket CodeForces
    AC日记——字符串的展开 openjudge 1.7 35
    AC日记——回文子串 openjudge 1.7 34
    AC日记——判断字符串是否为回文 openjudge 1.7 33
    AC日记——行程长度编码 openjudge 1.7 32
    AC日记——字符串P型编码 openjudge 1.7 31
    AC日记——字符环 openjudge 1.7 30
    AC日记——ISBN号码 openjudge 1.7 29
    AC日记——单词倒排 1.7 28
  • 原文地址:https://www.cnblogs.com/yanzi-meng/p/9273600.html
Copyright © 2011-2022 走看看