lua基金会【五岁以下儿童】I/O文件操作

--[[

lua操作相关文件I/O
]]--

--件,假设该文件不存在的话,
--lua会帮助我们在你规定的文件夹下创建这个文件,前提是该文件夹要存在
--[[
同一时候我们应该掌握写入文件的模式;

对下面写入模式进行说明:
"r" 模式:读模式(该模式下,仅仅同意对文件进行读取内容,不容许写入)

"w":写模式(同意对文件进行写入。上次的文件内容会由于本次的写入而被替换掉)

"a":加入模式

"w+":更新模式,全部之前的数据将被清除

"a+":更新模式,全部之前的数据将被保存,同一时候数据仅仅被同意在文件尾部加入
]]--

theFile =io.open("C:/Users/Administrator/Desktop/2.lua","w")
if theFile~=nil then
	theFile:write("this is a lua  file
")
	
	theFile:write("I know
")

    theFile:write("this  is my  test
")
	
	--文件在操作完以后记得关闭它
	io.close(theFile)
	--文件一旦被关闭,就必须重新启动它,才干够继续输入,可是之前的而文件内容会被覆盖掉
	myFile =io.open("C:/Users/Administrator/Desktop/2.lua","a+")
	
	y=myFile:seek("set",0)
	myFile:write("it works
")
	myFile:write("we are good friends")
	z=myFile:seek("cur")
	x =myFile:seek("end")
	print(x,y,z)
	myFile:setvbuf("no")
	myFile:flush()
	myFile:close(myFile)
end


--按行输出文件内容
for line in io.lines("C:/Users/Administrator/Desktop/2.lua") do
	print(line)
end




	--[[
    文件读取

    ]]--
    buf =myFile:read("*all")
	print(buf)



 

版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/mfrbuaa/p/4750868.html