object:close()

Type Function
Object File
Library io.*
Return value none
Revision 2017.3060
Keywords close, files
See also io.open()

Overview

Closes the open file (file is returned from io.open()).

Note that files are automatically closed when their handles are garbage collected, but that takes an unpredictable amount of time to happen.

Syntax

File:close()

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

file = nil