Trouble writing to file with applicationDataDirectory
I'm trying to write to a file when I'm on the device using applicationDataDirectory, but it doesn't seem to work with my code. Created an if statement with resourcesDirectory to be able to test it out in the simulator which works, but on the device (iPhone) no.
Any advice? Does my file which I write to (varor.txt) need to be put into a special folder or can it just reside inside of the Resources dir?
code:
http://pastie.org/1310619
3 Answers
-
The Resources directory is read-only, so while you can use it to read a file which has a default set of data, you won't be able to f.write() to it later.
To store the changes in the manner you want you need to name a file in the applicationDataDirectory (if you want the file to persist and get backed up) or in the tempDirectory (if you don't care if the file persists between executions and don't need it backed up).
-
var filename = "varor.txt"; var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename); if(f.exists()) { f.deleteFile(); } f.write("text for file");
-
>what I'm after is to have a file in applicationDataDirectory, but where do I place that folder? Inside the Resources folder or in the root of the project? and what do I call it?
You don't name or create the folder. Titanium automagically creates that folder for you within your application sandbox. Just use Ti.Filesystem.applicationDataDirectory, and it will map to the correct folder name depending on which OS you are using (or simmulator, etc).
I suggest you review the sample code in Kitchen Sink's filesystem.js file. See also the code snippet posted by General Usage.