Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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

— asked November 19th 2010 by Isak Anklew
  • applicationdatadirectory
  • iphone
  • mobile
0 Comments

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).

    — answered November 19th 2010 by Doug Handy
    permalink
    1 Comment
    • Yeah I gathered as much and 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?

      — commented November 19th 2010 by Isak Anklew
  • var filename = "varor.txt";
    
    var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename);
    
    if(f.exists()) { f.deleteFile(); }
    
    f.write("text for file");
    
    — answered November 19th 2010 by General Usage
    permalink
    0 Comments
  • >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.

    — answered November 19th 2010 by Doug Handy
    permalink
    1 Comment
    • Thanks for the answers!

      — commented November 19th 2010 by Isak Anklew
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.