io.close()

Type Function
Library io.*
Return value none
Revision 2017.3060
Keywords io, close, file
See also io.open()
io.read()

Overview

Closes an open file handle. Equivalent to file:close(). If a file handle is not specified, this function closes the default output file.

Syntax

io.close( [file] )
file (optional)

Object. Handle of file (as returned from io.open) to be closed.

Example

-- 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