I/O庫用于在Lua中讀取和處理文件。 Lua中有兩種文件操作,即隱式(Implicit)和顯式(Explicit)操作。
對于以下示例,無涯教程將使用例文件test.lua,如下所示。
-- sample test.lua -- sample2 test.lua
一個(gè)簡單的文件打開操作使用以下語句。
file=io.open (filename [, mode])
下表列出了各種文件模式。
Sr.No. | Mode & Remark |
---|---|
1 | " r" 只讀模式,是打開現(xiàn)有文件的默認(rèn)模式。 |
2 | " w" 啟用寫模式,該模式將覆蓋現(xiàn)有文件或創(chuàng)建新文件。 |
3 | " a" 追加模式,用于打開現(xiàn)有文件或創(chuàng)建要追加的新文件。 |
4 | " r +" 現(xiàn)有文件的讀寫模式。 |
5 | " w +" 如果文件存在或具有讀寫權(quán)限的新文件被刪除,則所有現(xiàn)有數(shù)據(jù)都將被刪除。 |
6 | " a +" 啟用了讀取模式的追加模式可以打開現(xiàn)有文件或創(chuàng)建新文件。 |
隱式操作
隱式(Implicit)文件描述符使用標(biāo)準(zhǔn)輸入/輸出模式,或使用單個(gè)輸入和單個(gè)輸出文件。下面顯示了使用隱式文件描述符的示例。
-- Opens a file in read file=io.open("test.lua", "r") -- sets the default input file as test.lua io.input(file) -- prints the first line of the file print(io.read()) -- closes the open file io.close(file) -- Opens a file in append mode file=io.open("test.lua", "a") -- sets the default output file as test.lua io.output(file) -- appends a word test to the last line of the file io.write("-- End of the test.lua file") -- closes the open file io.close(file)
運(yùn)行該程序時(shí),將獲得test.lua文件第一行的輸出。
-- Sample test.lua
這是test.lua文件中語句的第一行。同樣,"-test.lua文件的結(jié)尾"行將附加到test.lua代碼的最后一行。
在上面的示例中,您可以看到使用io。上面的示例使用不帶可選參數(shù)的io.read()。可選參數(shù)可以是以下任意一個(gè)。
Sr.No. | Mode & Remark |
---|---|
1 | " * n" 從當(dāng)前文件位置讀取并返回一個(gè)數(shù)字(如果文件位置存在)或返回nil。 |
2 | " * a" 從當(dāng)前文件位置返回文件的所有內(nèi)容。 |
3 | " * l" 從當(dāng)前文件位置讀取該行,并將文件位置移至下一行。 |
4 | number 讀取函數(shù)中指定的字節(jié)數(shù)。 |
其他常見的I/O方法包括
io.tmpfile()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? - 返回用于讀取和寫入的臨時(shí)文件,一旦程序退出,該文件將被刪除。
io.type(file)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? - 根據(jù)輸入文件返回file,close file還是nil。
io.flush()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-? 清除默認(rèn)輸出緩沖區(qū)。
io.lines(optional file name)? - 提供通用的 for 循環(huán)迭代器,循環(huán)遍歷文件并最終關(guān)閉文件,以防萬一在循環(huán)末尾提供了文件名未關(guān)閉該文件。
顯式操作
無涯教程經(jīng)常使用顯式(Explicit)文件描述符,該描述符允許一次處理多個(gè)文件。這些函數(shù)與隱式文件描述符非常相似。在這里使用file:function_name而不是io.function_name。下面顯示了相同隱式文件描述符示例的文件版本的以下示例。
-- Opens a file in read mode file=io.open("test.lua", "r") -- prints the first line of the file print(file:read()) -- closes the opened file file:close() -- Opens a file in append mode file=io.open("test.lua", "a") -- appends a word test to the last line of the file file:write("--test") -- closes the open file file:close()
運(yùn)行程序時(shí),您將得到與隱式描述符示例類似的輸出。
-- Sample test.lua
外部描述符的所有文件打開模式和參數(shù)讀取方式與隱式文件描述符相同。
其他常見的文件方法包括
file:seek(optional whence,optional offset)? ? ?-? ?whence參數(shù)為" set"," cur"或" end"。從文件開頭設(shè)置具有更新文件位置的新文件指針。該函數(shù)的偏移量從零開始。如果第一個(gè)參數(shù)為" set",則從文件開頭開始偏移;如果它是" cur";或從文件末尾開始(如果是" end")。默認(rèn)參數(shù)值為" cur"和0,因此可以通過不帶參數(shù)調(diào)用此函數(shù)來獲取當(dāng)前文件位置。
file:flush()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-? ?清除默認(rèn)輸出緩沖區(qū)。
io.lines(optional file name)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-? ?提供 for 循環(huán)迭代器,循環(huán)遍歷文件并最終關(guān)閉文件,以防萬一在循環(huán)末尾提供了文件名未關(guān)閉該文件。
下面顯示了使用seek方法的示例。它使光標(biāo)從文件結(jié)尾之前的25個(gè)位置偏移。讀取函數(shù)從搜索位置打印文件的其余部分。
-- Opens a file in read file=io.open("test.lua", "r") file:seek("end",-25) print(file:read("*a")) -- closes the opened file file:close()
您將獲得類似于以下內(nèi)容的輸出。
sample2 test.lua --test
您可以試玩所有不同的模式和參數(shù),以了解Lua文件操作的全部函數(shù)。文章來源:http://www.zghlxwxcb.cn/news/detail-631459.html
Lua - 文件I/O - 無涯教程網(wǎng)無涯教程網(wǎng)提供I/O庫用于在Lua中讀取和處理文件。 Lua中有兩種文件操作,即隱式(Implicit )和顯式(Ex...https://www.learnfk.com/lua/lua-file-io.html文章來源地址http://www.zghlxwxcb.cn/news/detail-631459.html
到了這里,關(guān)于無涯教程-Lua - 文件I/O的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!