------------------簡單模型-----------------
--讀
local file1=io.input("1.txt") --當前目錄"1.txt"要存在,不然出錯
local str=io.read("*a")
print(str)
--寫
local file2=io.output("2.txt") --當前目錄"2.txt"不需要存在
io.write(str)
io.flush()
io.close()
--利用這幾個函數可以做一個文件復制的函數
function copy(fileA,fileB)
local file1=io.input(fileA)
if not file1 then print(fileA.."不存在") return end
local str=io.read("*a")
local file2=io.output(fileB)
io.write(str)
io.flush()
io.close()
end
for line in io.lines("1.txt") do
print(line)
end
------------------完整模型-----------------
local f=io.open("3.txt","a+")
f:write("Happy New Year!")
f:flush()
f:seek("end",-1) --定位到文件末尾前一個字節(jié)
local str=f:read(1) --讀取一個字符
print(str) --輸出"!"
f:close()