lua的io包,文件读写

-- 以只读方式打开文件

-- io.open(filename string, mode)

-- filename 文件名

-- mode 打开文件模式

local file = io.open("1.txt", "r")

print(file)

-- 设置默认输入文件为 1.txt

-- io.input(...) 输人函数,读取文件时使用

io.input(file)--读取是使用

-- io.read(...)

-- "a" 读取整个文件

-- "l"读取下一行,丢弃换行符

-- "L" 读取下一行,保留换行符

-- "n" 读取一个数值

--  num 以字符串读取num个字符

while true do

    local str = io.read("L")

    if str then

        print(str)  

    else

        break

    end    

   

end

-- 关闭打开的文件

io.close(file)

-- 以附加的方式打开只写文件

file = io.open("1.txt", "a")

-- -- 设置默认输出文件为 1.txt

io.output(file)

-- -- 在文件最后一行添加 Lua 注释

io.write("--  1.txt 文件末尾注释")

-- -- 关闭打开的文件

io.close(file)

-- io.lines(filename string, ...)

-- filename文件名

-- ... 参数一次读取多少个数

for line in io.lines("1.txt") do

    print(line)

end

----上面是lua的io读写文件,下面是file直接操作

file = io.open("1.txt", "a")

--file:write()

--写文件

file:write("3333")

file:close()

file = io.open("1.txt", "r")

--file:read()

--读文件

print(file:read())

--file:close()

--关闭文件句柄

file:close()

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值