Advertisement
lvs

save

lvs
Jul 6th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.92 KB | None | 0 0
  1. function app.saveFile(strFilename, strValue, dir)
  2.     local path = system.pathForFile(strFilename, dir or system.DocumentsDirectory)
  3.     local file = io.open(path, 'w+')
  4.     if file then
  5.        file:write(strValue)
  6.        io.close(file)
  7.     end
  8. end
  9.  
  10. function app.readFile(strFilename, dir)
  11.     local theFile = strFilename
  12.     local path = system.pathForFile( theFile, dir or system.DocumentsDirectory )
  13.     -- io.open opens a file at path. returns nil if no file found
  14.     local file = io.open( path, 'r')
  15.     if file then
  16.        -- read all contents of file into a string
  17.        local contents = file:read('*a')
  18.        io.close( file )
  19.        return contents
  20.     else
  21.        return ''
  22.     end
  23. end
  24.  
  25. function app.readJsonFile(strFilename, dir)
  26.     return json.decode(app.readFile(strFilename, dir))
  27. end
  28.  
  29. function app.saveJsonFile(strFilename, strValue, dir)
  30.    app.saveFile(strFilename, json.encode(strValue), dir)
  31. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement