[Lua]Lua IO库整理

来源:互联网 发布:淘宝网电脑不能登录 编辑:程序博客网 时间:2024/05/20 10:12

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个字符到串

[cpp] view plain copy
  1. --[[test.lua的内容  
  2. hello world,I is test.lua  
  3. print(123)  
  4. --]]  
  5.  io.input("E:\\workplace\\project\\server\\script\\test\\src\\test.lua")  
  6.  t=io.read("*all")  
  7.  io.write(t, '\n')  ------输出整个 test.lua 文件的内容到 stdin  
  8. --> --hello world,I is test.lua  
  9. --> 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不是一个文件句柄

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


[cpp] view plain copy
  1. -- Opens a file in read  
  2. local file = io.open("E:\\workplace\\project\\server\\script\\test\\src\\test.lua""r")  
  3. io.input(file)--设置当前文件为默认输出文件  
  4. print(io.read())--默认读取第一行   
  5. --> --hello world,I is test.lua  
  6. io.close(file)--关闭  
  7.   
  8. -- Opens a file in append mode  
  9. file = io.open("E:\\workplace\\project\\server\\script\\test\\src\\test.lua""a")  
  10. -- sets the default output file as test.lua  
  11. io.output(file)  
  12. -- appends a word test to the last line of the file  
  13. io.write("-- End of the test.lua file\n")  
  14. -- closes the open file  
  15. io.close(file)  
  16.   
  17. --[[操作前  
  18. --hello world,I is test.lua  
  19. print(123)  
  20. --]]  
  21. --[[操作后  
  22. --hello world,I is test.lua  
  23. print(123)-- End of the test.lua file  
  24. --]]  

完全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将自动调整为最佳的大小

 

[cpp] view plain copy
  1. -- Opens a file in read mode  
  2. file = io.open("E:\\workplace\\project\\server\\script\\test\\src\\test.lua""r")  
  3. -- prints the first line of the file  
  4. print(file:read())  
  5. --> --hello world,I is test.lua  
  6. -- closes the opened file  
  7. file:close()  
  8.   
  9. -- Opens a file in append mode  
  10. file = io.open("E:\\workplace\\project\\server\\script\\test\\src\\test.lua""a")  
  11. -- appends a word test to the last line of the file  
  12. file:write("--test\n")  
  13. -- closes the open file  
  14. file:close()  
  15.   
  16. --[[操作前  
  17. --hello world,I is test.lua  
  18. print(123)-- End of the test.lua file  
  19. --]]  
  20. --[[操作后  
  21. --hello world,I is test.lua  
  22. print(123)-- End of the test.lua file  
  23. --test  
  24. --]]  

Lua IO库扩展

[plain] view plain copy
  1. --------------------------------  
  2. -- @module io  
  3.   
  4. -- start --  
  5.   
  6. --------------------------------  
  7. -- 检查指定的文件或目录是否存在,如果存在返回 true,否则返回 false  
  8. -- @function [parent=#io] exists  
  9. -- @param string path 要检查的文件或目录的完全路径  
  10. -- @return boolean#boolean   
  11.   
  12. --[[--  
  13.   
  14. 检查指定的文件或目录是否存在,如果存在返回 true,否则返回 false  
  15.   
  16. 可以使用 cc.FileUtils:fullPathForFilename() 函数查找特定文件的完整路径,例如:  
  17.   
  18. ~~~ lua  
  19.   
  20. local path = cc.FileUtils:getInstance():fullPathForFilename("gamedata.txt")  
  21. if io.exists(path) then  
  22.     ....  
  23. end  
  24.   
  25. ~~~  
  26.   
  27. ]]  
  28.   
  29. -- end --  
  30.   
  31. function io.exists(path)  
  32.     local file = io.open(path, "r")  
  33.     if file then  
  34.         io.close(file)  
  35.         return true  
  36.     end  
  37.     return false  
  38. end  
  39.   
  40. -- start --  
  41.   
  42. --------------------------------  
  43. -- 读取文件内容,返回包含文件内容的字符串,如果失败返回 nil  
  44. -- @function [parent=#io] readfile  
  45. -- @param string path 文件完全路径  
  46. -- @return string#string   
  47.   
  48. --[[--  
  49.   
  50. 读取文件内容,返回包含文件内容的字符串,如果失败返回 nil  
  51.   
  52. io.readfile() 会一次性读取整个文件的内容,并返回一个字符串,因此该函数不适宜读取太大的文件。  
  53.   
  54. ]]  
  55.   
  56. -- end --  
  57.   
  58. function io.readfile(path)  
  59.     local file = io.open(path, "r")  
  60.     if file then  
  61.         local content = file:read("*a")  
  62.         io.close(file)  
  63.         return content  
  64.     end  
  65.     return nil  
  66. end  
  67.   
  68. -- start --  
  69.   
  70. --------------------------------  
  71. -- 以字符串内容写入文件,成功返回 true,失败返回 false  
  72. -- @function [parent=#io] writefile  
  73. -- @param string path 文件完全路径  
  74. -- @param string content 要写入的内容  
  75. -- @param string mode 写入模式,默认值为 "w+b"  
  76. -- @return boolean#boolean   
  77.   
  78. --[[--  
  79.   
  80. 以字符串内容写入文件,成功返回 true,失败返回 false  
  81.   
  82. "mode 写入模式" 参数决定 io.writefile() 如何写入内容,可用的值如下:  
  83.   
  84. -   "w+" : 覆盖文件已有内容,如果文件不存在则创建新文件  
  85. -   "a+" : 追加内容到文件尾部,如果文件不存在则创建文件  
  86.   
  87. 此外,还可以在 "写入模式" 参数最后追加字符 "b" ,表示以二进制方式写入数据,这样可以避免内容写入不完整。  
  88.   
  89. **Android 特别提示:** 在 Android 平台上,文件只能写入存储卡所在路径,assets 和 data 等目录都是无法写入的。  
  90.   
  91. ]]  
  92.   
  93. -- end --  
  94.   
  95. function io.writefile(path, content, mode)  
  96.     mode = mode or "w+b"  
  97.     local file = io.open(path, mode)  
  98.     if file then  
  99.         if file:write(content) == nil then return false end  
  100.         io.close(file)  
  101.         return true  
  102.     else  
  103.         return false  
  104.     end  
  105. end  
  106.   
  107. -- start --  
  108.   
  109. --------------------------------  
  110. -- 拆分一个路径字符串,返回组成路径的各个部分  
  111. -- @function [parent=#io] pathinfo  
  112. -- @param string path 要分拆的路径字符串  
  113. -- @return table#table   
  114.   
  115. --[[--  
  116.   
  117. 拆分一个路径字符串,返回组成路径的各个部分  
  118.   
  119. ~~~ lua  
  120.   
  121. local pathinfo  = io.pathinfo("/var/app/test/abc.png")  
  122.   
  123. -- 结果:  
  124. -- pathinfo.dirname  = "/var/app/test/"  
  125. -- pathinfo.filename = "abc.png"  
  126. -- pathinfo.basename = "abc"  
  127. -- pathinfo.extname  = ".png"  
  128.   
  129. ~~~  
  130.   
  131. ]]  
  132.   
  133. -- end --  
  134.   
  135. function io.pathinfo(path)  
  136.     local pos = string.len(path)  
  137.     local extpos = pos + 1  
  138.     while pos > 0 do  
  139.         local b = string.byte(path, pos)  
  140.         if b == 46 then -- 46 = char "."  
  141.             extpos = pos  
  142.         elseif b == 47 then -- 47 = char "/"  
  143.             break  
  144.         end  
  145.         pos = pos - 1  
  146.     end  
  147.   
  148.     local dirname = string.sub(path, 1, pos)  
  149.     local filename = string.sub(path, pos + 1)  
  150.     extpos = extpos - pos  
  151.     local basename = string.sub(filename, 1, extpos - 1)  
  152.     local extname = string.sub(filename, extpos)  
  153.     return {  
  154.         dirname = dirname,  
  155.         filename = filename,  
  156.         basename = basename,  
  157.         extname = extname  
  158.     }  
  159. end  
  160.   
  161. -- start --  
  162.   
  163. --------------------------------  
  164. -- 返回指定文件的大小,如果失败返回 false  
  165. -- @function [parent=#io] filesize  
  166. -- @param string path 文件完全路径  
  167. -- @return integer#integer   
  168.   
  169. -- end --  
  170.   
  171. function io.filesize(path)  
  172.     local size = false  
  173.     local file = io.open(path, "r")  
  174.     if file then  
  175.         local current = file:seek()  
  176.         size = file:seek("end")  
  177.         file:seek("set", current)  
  178.         io.close(file)  
  179.     end  
  180.     return size  
  181. end  
这些quick_cocos中的
原创粉丝点击