Write to a file

Added by iNTERFACEWARE

How to open a file and write (replace file content), or append data (keep file content)

Source Code
   -- Write new content in a file - previous content is lost
   
   -- get a file handle - using overwrite mode
   local f = io.open('test2', 'w')
   f:write('This replaces previous text in the file')
   -- close the file handle
   f:close()
   
   -- read file to confirm data is overwritten
   local f = io.open('test2')   
   f:read('*a')
   f:close()
   
   
   -- Append content to a file - previous content is kept
   
   -- get a file handle - using append mode
   local f = io.open('test2', 'a')
   f:write(' - this is appended')
   -- close the file handle
   f:close()
   
   -- read file to confirm data is appended
   local f = io.open('test2')   
   f:read('*a')
   f:close()
Description
How to open a file and write (replace file content), or append data (keep file content)