发帖
充值
 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
0 0

分享源码5-替换指定行内容

源码分享 6533 0 2024-8-10 10:16:47
本帖最后由 lpopbxjz 于 2024-8-10 11:03 编辑

唉..  最近想写个安卓项目,没用按键 直接用的懒人,结果发现啥啥都没有,论坛上很多都找不到..这次项目用到的一些基础代码就发出来吧,希望懒人越来越好...

留下评论支持,谢谢,看着有评论也心情好哎,以便下次再次分享...

function 替换指定行内容(路径,行数,新内容)
        
        local file = io.open(路径, "r")
        if not file then
                error("文件无法打开")
        end
        
        local lines = {}
        for line in file:lines() do
                lines[#lines + 1] = line
        end
        file:close()
        
        if 行数 < 1 or 行数 > #lines then
                error("行号超出文件范围")
        end
        
        lines[行数] = 新内容 -- 替换指定行
        
        file = io.open(路径, "w")
        if not file then
                error("文件无法打开")
        end
        
        for i, line in ipairs(lines) do
                file:write(line .. "\n") -- 写回文件,保留换行符
        end
        file:close()
end

使用道具 举报

您需要登录后才可以回帖 立即登录
高级模式
返回
源码分享