lua 读写文件

--I/O库为文件操作提供2个里一个输入库和一个输出库io.read()

--io.write() 该函数将所有参数按照顺序写到当前输出文件中
FILE_NAME = 'c:/1.txt'
FILE_NAME2 = '
'

function write()
  io.write('hello ', 'world')
end

--write()

--io.read() 读取当前文件的内容 "*all" "*line" "*number" number
--[[for count = 1,math.huge do
    local line = io.read("*line")  --如果不传参数,缺省值也是"*line"
    if line == nil then
        break
    end
    io.write(string.format("%6d  ",count),line,"\n")
end--]]

--读取指定文件
function getFile(file_name)
  local f = assert(io.open(file_name, 'r'))
  local string = f:read("*all")
  f:close()
  return string
end

-- local lines,rest = f:read(BUFSIZE,"*line")
function getFileLine(file_name)
  local BUFSIZE = 84012
  local f = assert(io.open(file_name, 'r'))
  local lines,rest = f:read(BUFSIZE, "*line")
  f:close()
  return lines , rest
end

--字符串写入
function writeFile(file_name,string)
 local f = assert(io.open(file_name, 'w'))
 f:write(string)
 f:close()
end

writeFile(FILE_NAME2, getFile(FILE_NAME))
--控制台写入字符串到文件中
function writeFile2(file_name)
 local f = assert(io.open(file_name, 'w'))
 local string = io.read()
 f:write(string)
 f:close()
end
io.write()
writeFile2(FILE_NAME2)
复制去Google翻译 翻译结果
ME2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值