Lua讀寫文件代碼示例
更新時間:2015年04月22日 09:35:51 投稿:junjie
這篇文章主要介紹了Lua讀寫文件代碼示例,本文講解了讀寫文件的模式以及讀寫文件代碼實例,需要的朋友可以參考下
讀寫文件的模式:
復制代碼 代碼如下:
r - 讀取模式w - 寫入模式(覆蓋現有內容)
a - 附加模式(附加在現有內容之后)
b - 二進制模式
r+ - 讀取更新模式(現有數據保留)
w+ - 寫入更新模式(現有數據擦除)
a+ - 附加更新模式(現有數據保留,只在文件末尾附加)
do --read data from file function readFile() local fileHandle = assert(io.open("test.txt", "r"), "not the file"); if fileHandle then local outData = fileHandle:read("*all"); print(outData); else print("false"); end fileHandle:close(errorInfo); end --write data to the file function writeFile(dataBuffer) local writeHandle = assert(io.open("write.txt", "a+"), "not the file"); if writeHandle then writeHandle:write(dataBuffer); print("true"); else print("false"); end writeHandle:close(); end local inputData = 0; repeat inputData = io.read(); --write the data from io writeFile(inputData); until inputData == '#' end
您可能感興趣的文章: