高速掌握Lua 5.3 —— I/O库 (1)

Q:什么是”Simple Model”?

A:全部的文件操作都基于一个默认的输入文件和一个默认的输出文件。这就意味着同一时间对于输入和输出来说,仅仅可操作一个文件(默认的文件)。

默认的输入文件初始化是stdin,默认的输出文件初始化是stdout

-- "a.lua"文件里:
--[[ "io.read()"从"io.input()"所指定的默认输入文件里读;
     "io.write()"向"io.output()"所指定的默认输出文件里写。

由于默认输入文件初始化是"stdin";默认的输出文件初始化是"stdout", 所以"io.read()"从"stdin"读;"io.write()"向"stdout"写。]] io.write(io.read()) --[[ result: > lua a.lua Hello World! Hello World!]]

io.read()io.write()都能够接收多个參数。

-- "a.lua"文件里:
io.write(io.read(), " and", " The Complete I/O Model.")
--[[ result: 
     > lua a.lua
     The Simple I/O Model
     The Simple I/O Model and The Complete I/O Model.]]

io.input()io.output()能够更改相应的默认文件。

-- "file1.txt"文件里:
Hello World!
-- "a.lua"文件里:
local temp_input = io.input()    -- 保存原先的默认输入文件(此时是"stdin")。
local temp_output = io.output()    -- 保存原先的默认输出文件(此时是"stdout")。
io.input("file1.txt")    -- 指定默认的输入文件为"file1.txt"。
io.output("file2.txt")    -- 指定默认的输出文件为"file2.txt"。
io.write(io.read())
io.input():close()    -- 关闭"file1.txt"。
io.output():close()    -- 关闭"file2.txt"。
io.input(temp_input)    -- 恢复原先的默认输入文件。

io.output(temp_output) -- 恢复原先的默认输出文件。 --[[ result: > lua a.lua > cat file2.txt Hello World!]]

Q:print()io.write()有什么差别?

A:
1、print()在打印其每一个參数之间添加 ,且最后添加 。而io.write()均不会。

print("hello", "Lua")
print("Hi")
--[[ result: 
     hello    Lua
     Hi]]
io.write("hello", "Lua")
io.write("Hi", "
")
--> helloLuaHi

2、io.write()io.output()指定的文件里写(初始值是stdout)。而print()总向stdout写。

-- "a.lua"文件里:
local temp = io.output()
io.output("file3.txt")
io.write("Hello")
print(" World")
io.output():close()
io.output(temp)
--[[ result: 
     "file3.txt"文件里:
     Hello
     "stdout":
      World]]

3、print()对于不能被直接打印的对象会自己主动调用tostring()转换。而io.write()不会。

t = {}
print(t)    --> table: 0x1ca9aa0
io.write(t)    --> error

Q:io.read()的參数?

A:

-- "file1.txt"文件里:
line1
line2
line3

-- "data.txt"文件里:
6.0       -3.23     15e12
4.3       234       1000001

1、*a从当前的文件读取位置读取到文件尾,假设文件为空或者已在文件尾,则返回空串,

local temp = io.input()
io.input("file1.txt")
io.write(io.read("*a"))
io.input():close()
io.input(temp)
--[[ result: 
     line1
     line2
     line3]]

2、*l从当前的文件读取位置读取到行尾,不包括结尾的 *L*l功能同样,但包括行尾的 。假设文件为空或者已在文件尾,则返回nil

local temp = io.input()
io.input("file1.txt")
str = ""
local i = 1
while true do
    local s
    if i == 2 then 
        s = io.read("*L")
    else
        s = io.read("*l")    -- "io.read()"不指定參数时。默认使用"*l"參数。
    end
    if s == nil then break end    -- 当文件读取完时,返回"nil"。

str = str .. s i = i + 1 end io.write(str) --[[ result: line1line2 line3]] io.input():close() io.input(temp)

3、*n从当前的文件读取位置读取一个数值。io.read("*n")返回数值,而不是字符串。

当须要从一个文件里读取大量数字时,数字间的字符串为空格能够显著提高执行性能。io.read("*n")会跳过两个可被识别数字之间的随意空格。假设在当前位置找不到一个数字(由于格式不正确,或者是到了文件的结尾)。则返回nil

-- 打印文件每一行中最大的数字。
local temp = io.input()
io.input("data.txt")
while true do
    local n1, n2, n3 = io.read("*n", "*n", "*n")
    if not n1 then break end
    print(math.max(n1, n2, n3))
end
io.input():close()
io.input(temp)
--[[ result: 
     15000000000000.0
     1000001]]

4、io.read(num)能够指定一个数字,lua将尽可能读取”num”个字符(假设不够则能读多少读多少),假设文件为空或者已读取到文件尾则返回nil

local temp = io.input()
io.input("data.txt")
io.write(io.read(3))    -- 读取3个字符。
print()
-- 读取100个字符。

但实际没有那么多。所以能读取多少读多少。 io.write(io.read(100)) io.input():close() io.input(temp) --[[ result: 6.0 -3.23 15e12 4.3 234 1000001]]

Q:其它的”Simple Model”模式的库函数?

A:

--[[ io.tmpfile()
     以"w+"的模式打开一个暂时文件,并返回其文件句柄。
     当程序执行结束时,该文件会被自己主动删除。

]] local f = io.tmpfile() f:write("123") f:seek("set", 0) -- 将文件的当前读取位置定位到文件头。 print(f:read()) -- 123 f:close() --[[ io.type(obj) 检查"obj"是否为有效的文件句柄。

函数返回例如以下: "file": 假设"obj"是一个被打开的文件句柄。 "closed file": 假设"obj"是一个被关闭的文件句柄。 nil: 假设"obj"不是一个有效的文件句柄。

]] print(io.type(f)) -- nil f = io.tmpfile() print(io.type(f)) -- file f:close() print(io.type(f)) -- closed file

附加:

1、io.input()io.output()在出错时均会报错。假设想控制错误。请使用”Complete model”下的io.open()
2、向io.write()传递的数字參数会被自己主动转换为字符串。

假设想全然控制转换,最好使用string.format()

io.write("sin(3) = ", math.sin(3), "
")
--> sin(3) = 0.14112000805987
io.write(string.format("sin(3) = %.4f
", math.sin(3)))
--> sin(3) = 0.1411

3、避免写io.write(a..b..c)这种代码。io.write(a, b, c)能够完成同样的工作,同一时候避免了..操作符复制字符串所耗费的内存资源。
4、当文件为空,或者文件当前的读取位置在文件尾时,io.read("*a")会返回空字符串,而io.read("*l")io.read("*L")io.read("*n")io.read(num)都会返回nil
5、假设想要逐行的读取文件,相比io.read("*l")。更好的选择是io.lines()io.lines()能够指定想要读取的文件。而且能够指定多个,依次的按行读取这些文件。

同一时候在读取完成后。io.lines()会自己主动关闭文件。

-- 为文件的每一行添加行号。
-- "file1.txt"文件里。
line1
line2
line3
-- "a.lua"文件里。

local count = 1 for line in io.lines("file1.txt") do io.write(string.format("%6d ", count), line, " ") count = count + 1 end --[[ result: > lua a.lua 1 line1 2 line2 3 line3]]

然而假设io.lines()不带參数,则与io.read()的功能同样,同一时候在读取完成后不会关闭文件。
6、假设对于文件须要逐块的操作。但终于会操作整个文件。

那么最好使用io.read("*a")读取整个文件,然后使用string.gmatch()分解出须要操作的块。

-- 上面打印文件每一行中最大的数字的样例。
-- 须要处理的块。一个或多个非空格加上一个或多个空格……
local block = "(%S+)%s+(%S+)%s+(%S+)%s+"
--[[ "string.gmatch()"返回的结果是字符串形式的,
     假设直接传递给"math.max()"会依照字符串形式比較大小,
     所以须要使用"tonumber()"转换为数字。]]
for n1, n2, n3 in string.gmatch(io.read("*a"), block) do
    print(math.max(tonumber(n1), tonumber(n2), tonumber(n3)))
end

省去了多次读文件的操作。效率会更高。
7、在Lua中复制文件的高效的方式,

local size = 2^13    -- good buffer size (8K)
while true do
    local block = io.read(size)
    if not block then break end
    io.write(block)
end

8、io.read(0)測试是否到了文件尾。假设未到,则返回空字符串,假设到了则返回nil

原文地址:https://www.cnblogs.com/jzssuanfa/p/7080932.html