Lua 处理文件

local fmt = string.format

local zfile = {}

function zfile.read(fpath, count)
    if (count == nil) then count = '*all' end
    if (fpath) then
        local fd = io.open(fpath, 'r')
        if (fd) then
            local content = fd:read(count)
            fd:close()
            return content
        else
            print(fmt('error> read file %s failed', fpath))
        end
    end
    return nil
end

function zfile.write(fpath, data)
    if (fpath and data) then
        local fd = io.open(fpath, 'w+')
        if (fd) then
            fd:write(data)
            fd:close()
            return true
        else
            print(fmt('error> write file %s failed', path))
        end
    end
    return false
end

function zfile.exists(fpath)
    local content = zfile.read(fpath)
    if (content) then
        return true
    end
    return false
end

return zfile

results matching ""

    No results matching ""