对象:lines()

类型 函数
对象 文件
io.*
返回值 函数 (迭代器)
版本 发行版 2024.3703
关键字 行,文件
另请参见 io.open()
io.lines()

概述

返回一个迭代器函数,每次调用时,都会返回文件中的一个新行。

此函数类似于 io.lines(),只是必须先打开文件(使用 io.open()),且不会在文件末尾自动关闭。

语法

File:lines()

示例

-- Path for the file to read
local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )

-- Open the file handle
local file, errorString = io.open( path, "r" )

if not file then
    -- Error occurred; output the cause
    print( "File error: " .. errorString )
else
    -- Output lines
    for line in file:lines() do
        print( line )
    end
    -- Close the file handle
    io.close( file )
end

file = nil