Write to an existing text file
Has any had any experience with this? so far i'm able list the contents of a directory, click a file name and open it's contents in to a textarea but am not able to save the updated content.
Thx!
3 Answers
-
Colton, here is how i'm doing it. Hope it helps…
function fn_save_file(data,path,title) // data to be saved, path to file, name of file { if(data) // make sure we have something to save { var file = Titanium.Filesystem.getFile(parent, path + title + '.txt'); // create file object if(file.write(data)) // this line does the saving! and if successful does something next { // do something after saving the file. I use it like a callback function } if(!file.write(data)) // tell user if save is unsuccessful { var save_error = Ti.UI.createAlertDialog({ title: 'SAVE ERROR', message: 'Could not save file', buttonNames: ['OK'] }); save_error.show(); } } }
-
Checkout the Mobile API's File Object for this. And also, the Kitchensink and Titanium Sandbox have examples of using this.
What you're looking for though is the Method or Property (i forget which) that specifies what kind of FileMode that you're opening the file as. Whether it's Binary, Append, Read, Write, etc… -
Kelly, from your Comment on the OP it seems you have figured this out? Could you explain how to do it, for people (like me…) who would like to know? :)