object:flush()

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

Overview

Commits the file's output buffer. Saves any written data to the file.

Syntax

File:flush()

Example

local path = system.pathForFile( "data.txt", system.DocumentsDirectory  )
local file, errorString = io.open( path, "w" )  -- open/create for write
 
if not file then
    -- Error occurred; output the cause
    print( "File error: " .. errorString )
end
 
-- The flush call is not really needed in this example but it would be good if
-- there is a long time between when data is written and when the file is closed

fh:write( "Line 1\n" )
fh:flush()  -- Save the data
 
fh:write( "Line 2\n" )
fh:flush()  -- Save the data
 
fh:write( "Line 3\n" )
 
fh:close()  -- Save all the data and close the file