readFile 将指定文件中的内容按行读取

--将指定文件中的内容按行读取
function readFile(path)
    local file = io.open(path,"r");
    if file then
        local _list = {};
        for l in file:lines() do
            table.insert(_list,l)
        end
        file:close();
        return _list
    end
end
参数 类型 必填 说明
path string 要读取文件的路径
返回值 类型 说明
list table/nil 文件内容

函数用例

--将指定文件中的内容按行读取
function readFile(path)
    local file = io.open(path,"r");
    if file then
        local _list = {};
        for l in file:lines() do
            table.insert(_list,l)
        end
        file:close();
        return _list
    end
end
--检测指定文件是否存在
function file_exists(file_name)
    local f = io.open(file_name, "r")
    return f ~= nil and f:close()
end
bool = file_exists(userPath().."/lua/main.lua")
if bool then
list = readFile(userPath().."/lua/main.lua")
if #list > 0 then
    for  i=1, #list,1  do
        dialog(list[i])
    end
end
    else
    dialog("文件不存在",0)
end
Copyright 北京帮你玩科技有限公司 2023 all right reserved,powered by Gitbook该文章修订时间: 2024-03-06 17:50:18

results matching ""

    No results matching ""