现在的位置: 首页 > 综合 > 正文

[Lua]Lua IO库整理

2019年01月08日 ⁄ 综合 ⁄ 共 4112字 ⁄ 字号 评论关闭

I/O库为文件操作提供了两种不同的模型,简单模型和完整模型。简单模型假设有一个当前输入文件和一个当前输出文件,它的I/O操作均作用于这些文件完整模型则使用显式地文件句柄。它采用了面向对象的风格,并将所有的操作定义为文件句柄上的方法。 

简单IO模式

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

用函数io.inputio.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"

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

<num>

读取num个字符到串

--[[test.lua的内容
hello world,I is test.lua
print(123)
--]]
 io.input("E:\\workplace\\project\\server\\script\\test\\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: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 (

)

 

io.output (

)

相当于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(...)

按指定的参数格式输出文件内容,参数必须为字符或数字,若要输出其它值,则需通过tostringstring.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
--]]

抱歉!评论已关闭.