对象:close()

类型 函数
对象 文件
io.*
返回值
修订版本 版本 2024.3703
关键词 关闭,文件
另请参阅 io.open()

概述

关闭打开的文件(文件由 io.open() 返回)。

请注意,当文件句柄被垃圾回收时,文件会自动关闭,但这需要不可预测的时间。

语法

File:close()

示例

-- Path for the file
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
    -- Read data from file
    local contents = file:read( "*a" )
    -- Output the file contents
    print( "Contents of " .. path .. "\n" .. contents )
    -- Close the file handle
    file:close()
end

file = nil