在Lua中,可以使用io庫(kù)來(lái)進(jìn)行文件讀寫(xiě)操作。以下是一些常用的文件讀寫(xiě)操作示例:
local file = io.open("file.txt", "r")
if file then
local content = file:read("*a")
print(content)
file:close()
else
print("Error: Unable to open file")
end
local file = io.open("file.txt", "w")
if file then
file:write("Hello, world!")
file:close()
else
print("Error: Unable to open file")
end
local file = io.open("file.txt", "a")
if file then
file:write("\nThis is a new line")
file:close()
else
print("Error: Unable to open file")
end
在進(jìn)行文件讀寫(xiě)操作時(shí),需要確保文件的讀寫(xiě)權(quán)限并在操作完成后關(guān)閉文件流。
億速云公眾號(hào)
手機(jī)網(wǎng)站二維碼
Copyright ? Yisu Cloud Ltd. All Rights Reserved. 2018 版權(quán)所有
廣州億速云計(jì)算有限公司粵ICP備17096448號(hào)-1 粵公網(wǎng)安備 44010402001142號(hào)增值電信業(yè)務(wù)經(jīng)營(yíng)許可證編號(hào):B1-20181529