io.close()

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

概述

关闭一个打开的文件句柄。等同于 file:close()。如果没有指定文件句柄,此函数将关闭默认输出文件。

语法

io.close( [file] )
file (可选)

对象. 要关闭的文件句柄(由 io.open 返回)。

示例

-- 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
    io.close( file )
end

file = nil