Lua IO库笔记

IO库提供了两套不同风格的文件处理接口。第一种风格为简单模式, 其使用隐式的文件句柄; 它提供设置默认输入文件已经默认输出文件的操作, 所有的输入输出操作都针对这些默认文件。第二种风格为完全模式,其使用显示的文件句柄

当使用隐式句柄时, 所有的操作都由表IO提供,当使用显示文件句柄, io.open会返回文件句柄, 且所有的操作都由该文件句柄的方法来提供。

表IO中也提供了三个和C中含义相同的预定义文件句柄:io.stdin、io.stdout、stderr. I/O库永远不会关闭这些文件

简单模式在做一些简单文件操作时比较合适。但是在进行一些高级的文件操作的时候就显得力不从心了,比如同时打开多个文件等。

除非另有说明, I/O函数出错时都会返回nil(第二个返回值为错误消息, 第三个返回值为系统相关的错误码)。 成功时返回与nil不同的值

简单IO模式

简单模型的所有操作都作用于两个当前文件。I/O库将当前输入文件初始化为进程标准输入(stdin),将当前输出文件初始化为进程标准输出。在执行io.read()操作时,就会从标准输入中读取一行。

用函数io.input和io.output可以改变这两个当前文件。io.input(filename)调用会以只读模式打开指定的文件,并将其设定为当前输入文件;除非再次调用io.input,否则所有的输入都将来源于这个文件;在输出方面,io.output也可以完成类似的工作


 io.write, io.read 是一对.默认情况下,他们从stdin读输入,输出到stdout。 另有两个函数可以改变这一默认行为: io.input("xx"), io.output("yy") 他们改变输入为某个 xx 文件, 输出到 yy 文件。 eg:


如果 io.read()参数

"*all"

从当前位置读取整个文件,若为文件尾,则返回空字串

"*line"

[默认]读取下一行的内容,若为文件尾,则返回nil

"*number"

读取指定字节数的字符,如果number为0则返回空字串,若为文件尾,则返回nil;

<num>

读取num个字符到串

--[[test.lua的内容
hello world,I is test.lua
print(123)
--]]
 io.input("E:\\workplace\\src\\test.lua")
 t=io.read("*all")
 io.write(t, '\n')  ------输出整个 test.lua 文件的内容到 stdin
--> --hello world,I is test.lua
--> print(123)


函数名

功能描述

io.close([file])

相当于file:close(),关闭默认的输出文件

io.flush()

相当于file:flush(),输出所有缓冲中的内容到默认输出文件

io.lines ([filename])

打开指定的文件filename为读模式并返回一个迭代函数,每次调用将获得文件中的一行内容,当到文件尾时,将返回nil,并自动关闭文件
若不带参数时io.lines() <=> io.input():lines(); 读取默认输入设备的内容,但结束时不关闭文件
如:for line in io.lines("main.lua") do
print(line)
end

io.input ([file])

io.output ([file])相当于io.input,但操作在默认输出文件上

io.read (...)

相当于io.input():read

io.write (...)

相当于io.output():write

io.tmpfile ()

返回一个临时文件句柄,该文件以更新模式打开,程序结束时自动删除

io.type (obj)

检测obj是否一个可用的文件句柄;返回:
"file":为一个打开的文件句柄
"closed file":为一个已关闭的文件句柄
nil:表示obj不是一个文件句柄

--print与io.write的不同
--Write函数与print函数不同在于,write不附加任何额外的字符到输出中去,例如制表符,换行符等等。还有write函数是使用当前输出文件,而print始终使用标准输出。另外print函数会自动调用参数的tostring方法,所以可以显示出表(tables)函数(functions)和nil。
print("hello", "Lua"); 
print("Hi")
--> hello   Lua
--> Hi
io.write("hello", "Lua"); 
io.write("Hi", "\n")
--> helloLuaHi


-- Opens a file in read
local file = io.open("E:\\workplace\\project\\server\\script\\test\\src\\test.lua", "r")
io.input(file)--设置当前文件为默认输出文件
print(io.read())--默认读取第一行 
--> --hello world,I is test.lua
io.close(file)--关闭
 
-- Opens a file in append mode
file = io.open("E:\\workplace\\project\\server\\script\\test\\src\\test.lua", "a")
-- sets the default output file as test.lua
io.output(file)
-- appends a word test to the last line of the file
io.write("-- End of the test.lua file\n")
-- closes the open file
io.close(file)
 
--[[操作前
--hello world,I is test.lua
print(123)
--]]
--[[操作后
--hello world,I is test.lua
print(123)-- End of the test.lua file
--]]

完全IO模式

简单I/O功能太受限了,以至于基本没有什么用处,而用的更多的则是这里说的完整I/O模型。完整I/O模型可以进行更多的I/O控制,它是基于文件句柄的,就好比与C语言中的FILE*,表示一个正在操作的文件。

要打开一个文件,可以使用io.open函数,它有两个参数,一个表示要打开的文件名,另一个表示操作的模式字符串。模式字符串可以有以下四种取值方式:io.open(filename,[mode])的mode取值

"r"

是只读方式打开(默认), 不能写入。

"w"

只写方式打开,不能读取。

"a"

追加打开一个现有的文件或进行追加创建一个新的文件模式。

"r+"

以读写方式打开,保留原有数据。这个模式是自由度最高的。

"w+"

以读写方式打开,删除原有数据。就是打开后文件是空文件。

"a+"

以读写方式打开,保留原有数据,只能在文件末尾添加,不能在文件中间改写数据。若找不到文件,也会创建新文件

"b"

某些系统支持二进制方式

正常情况下open函数返回一个文件的句柄。如果发生错误,则返回nil,以及一个错误信息和错误代码。  

当成功打开一个文件以后,就可以使用read/write方法读写文件了,这与read/write函数相似,但是需要用冒号语法,将它们作为文件句柄的方法来调用

函数名

功能描述

io.open (filename [, mode])

按指定的模式打开一个文件,成功则返回文件句柄,失败则返回nil+错误信息

file:close()

关闭文件
注:当文件句柄被垃圾收集后,文件将自动关闭。句柄将变为一个不可预知的值

file:flush()

向文件写入缓冲中的所有数据

file:lines()

返回一个迭代函数,每次调用将获得文件中的一行内容,当到文件尾时,将返回nil,但不关闭文件

如:for line in file:lines() do body end

file:read(...)

按指定的格式读取一个文件,按每个格式函数将返回一个字串或数字,如果不能正确读取将返回nil,若没有指定格式将指默认按行方式进行读取

file:write(...)

按指定的参数格式输出文件内容,参数必须为字符或数字,若要输出其它值,则需通过tostring或string.format进行转换

file:seek([whence][,offset])

置和获取当前文件位置,成功则返回最终的文件位置(按字节),失败则返回nil加错误信息whence:
"set": 从文件头开始
"cur": 从当前位置开始[默认]
"end": 从文件尾开始
offset:默认为0
不带参数file:seek()则返回当前位置,file:seek("set")则定位到文件头,file:seek("end")则定位到文件尾并返回文件大小

file:setvbuf(mode,[,size])

设置输出文件的缓冲模式mode:
"no": 没有缓冲,即直接输出
"full": 全缓冲,即当缓冲满后才进行输出操作(也可调用flush马上输出)
"line": 以行为单位,进行输出(多用于终端设备)
最后两种模式,size可以指定缓冲的大小(按字节),忽略size将自动调整为最佳的大小

 

-- Opens a file in read mode
file = io.open("E:\\workplace\\project\\server\\script\\test\\src\\test.lua", "r")
-- prints the first line of the file
print(file:read())
--> --hello world,I is test.lua
-- closes the opened file
file:close()
 
-- Opens a file in append mode
file = io.open("E:\\workplace\\project\\server\\script\\test\\src\\test.lua", "a")
-- appends a word test to the last line of the file
file:write("--test\n")
-- closes the open file
file:close()
 
--[[操作前
--hello world,I is test.lua
print(123)-- End of the test.lua file
--]]
--[[操作后
--hello world,I is test.lua
print(123)-- End of the test.lua file
--test
--]]

Lua IO库扩展

--------------------------------
-- 检查指定的文件或目录是否存在,如果存在返回 true,否则返回 false
-- @function [parent=#io] exists
-- @param string path 要检查的文件或目录的完全路径
-- @return boolean#boolean 

检查指定的文件或目录是否存在,如果存在返回 true,否则返回 false
可以使用 cc.FileUtils:fullPathForFilename() 函数查找特定文件的完整路径,例如:
local path = cc.FileUtils:getInstance():fullPathForFilename("gamedata.txt")
if io.exists(path) then
    ....
end

function io.exists(path)
    local file = io.open(path, "r")
    if file then
        io.close(file)
        return true
    end
    return false
end

--------------------------------
-- 读取文件内容,返回包含文件内容的字符串,如果失败返回 nil
-- @function [parent=#io] readfile
-- @param string path 文件完全路径
-- @return string#string 

读取文件内容,返回包含文件内容的字符串,如果失败返回 nil
io.readfile() 会一次性读取整个文件的内容,并返回一个字符串,因此该函数不适宜读取太大的文件。

function io.readfile(path)
    local file = io.open(path, "r")
    if file then
        local content = file:read("*a")
        io.close(file)
        return content
    end
    return nil
end

-- start --

--------------------------------
-- 以字符串内容写入文件,成功返回 true,失败返回 false
-- @function [parent=#io] writefile
-- @param string path 文件完全路径
-- @param string content 要写入的内容
-- @param string mode 写入模式,默认值为 "w+b"
-- @return boolean#boolean 

以字符串内容写入文件,成功返回 true,失败返回 false
"mode 写入模式" 参数决定 io.writefile() 如何写入内容,可用的值如下:

-   "w+" : 覆盖文件已有内容,如果文件不存在则创建新文件
-   "a+" : 追加内容到文件尾部,如果文件不存在则创建文件

此外,还可以在 "写入模式" 参数最后追加字符 "b" ,表示以二进制方式写入数据,这样可以避免内容写入不完整。

**Android 特别提示:** 在 Android 平台上,文件只能写入存储卡所在路径,assets 和 data 等目录都是无法写入的。

function io.writefile(path, content, mode)
    mode = mode or "w+b"
    local file = io.open(path, mode)
    if file then
        if file:write(content) == nil then return false end
        io.close(file)
        return true
    else
        return false
    end
end


--------------------------------
-- 拆分一个路径字符串,返回组成路径的各个部分
-- @function [parent=#io] pathinfo
-- @param string path 要分拆的路径字符串
-- @return table#table 
--拆分一个路径字符串,返回组成路径的各个部分
-- local pathinfo  = io.pathinfo("/var/app/test/abc.png")
-- 结果:
-- pathinfo.dirname  = "/var/app/test/"
-- pathinfo.filename = "abc.png"
-- pathinfo.basename = "abc"
-- pathinfo.extname  = ".png"

function io.pathinfo(path)
    local pos = string.len(path)
    local extpos = pos + 1
    while pos > 0 do
        local b = string.byte(path, pos)
        if b == 46 then -- 46 = char "."
            extpos = pos
        elseif b == 47 then -- 47 = char "/"
            break
        end
        pos = pos - 1
    end

    local dirname = string.sub(path, 1, pos)
    local filename = string.sub(path, pos + 1)
    extpos = extpos - pos
    local basename = string.sub(filename, 1, extpos - 1)
    local extname = string.sub(filename, extpos)
    return {
        dirname = dirname,
        filename = filename,
        basename = basename,
        extname = extname
    }
end

--------------------------------
-- 返回指定文件的大小,如果失败返回 false
-- @function [parent=#io] filesize
-- @param string path 文件完全路径
-- @return integer#integer 
function io.filesize(path)
    local size = false
    local file = io.open(path, "r")
    if file then
        local current = file:seek()
        size = file:seek("end")
        file:seek("set", current)
        io.close(file)
    end
    return size
end

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值