Lua程序员首页投稿(暂停使用,暂停投稿)

Lua io.write()

2016-07-24  本文已影响2154人  AlbertS

前言#

前一章讲解了io.read()的用法,这一章必然要看io.write()是怎么用的,因为读写不分家嘛,相比读文件有很多的形式参数,写文件可是要简单多了,就是向文件中写一个字符流,没有众多的参数形式。

内容#


Lua io.write()##


Usage##

-- 打开文件
local file = io.open("writetest.txt", "w")
if nil == file then
    print("open file writetest.txt fail")
end

-- 输入字符串
file:write("test io.write\n");

-- 输入数字
file:write(2016)

-- 输入分隔符
file:write(" ")

-- 继续输入数字
file:write(7)
file:write(" ")
file:write(23)
file:write("\n")

-- 继续输入其他类型
file:write(tostring(os.date()))
file:write("\n")
file:write(tostring(file))

-- 关闭文件
file:close()

-- 读取文件并显示
local fileread = io.open("writetest.txt", "r")

local content = fileread:read("*a");
print("file content is : \n")
print(content)

fileread:close()
io_write.png

总结#

上一篇下一篇

猜你喜欢

热点阅读