Type Function Object File Library io.* Return value none Revision Release 2022.3683 Keywords write, files See also io.open() Reading and Writing Files
Writes the value of each of its arguments to the file. The arguments must be Strings or Numbers. To write other values, use tostring()
or string.format() before calling File:write()
.
For security reasons, you are not allowed to write files in the system.ResourceDirectory
(the directory where the application is stored). You must specify either system.DocumentsDirectory
, system.TemporaryDirectory
, or system.CachesDirectory
in the system.pathForFile() function when opening the file for writing.
File:write( arg1 [, arg2] [, ...] )
String. One or more strings to be written to the file represented by the File
object.
-- Data (string) to write local saveData = "My app state data" -- Path for the file to write local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory ) -- Open the file handle local file, errorString = io.open( path, "w" ) if not file then -- Error occurred; output the cause print( "File error: " .. errorString ) else -- Write data to file file:write( saveData ) -- Close the file handle io.close( file ) end file = nil