Titanium Community Questions & Answer Archive

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

Copy a file...

I have a File at: ../Resources/an/default.json
I want to copy this file to applicationDataDirectory

is there a simple way of doing this?

just FYI… This is a default file that I use to setup a new system when it first loads up.
After this file is loaded, then I would be doing refreshes of the file over the net.

related question: the ResourceDirectoy is readonly, but can I put this data in the Resources directory?

— asked November 16th 2010 by vincent youmans
  • applicationdatadirectory

4 Answers

  • To expand on Hal's answer and more directly address the main question, you use getFile() to obtain a file object for both the source and destination files. Then you use f.write() on the destination file, and pass it f.read() on the source file. For example, let's say you wanted to copy FooBar.txt from your resource directory to your data directory, but only once. (That is, if a previoius program execution had already done it, you want to retain that copy in case your app modifies it.)

    var myFilename = 'default.json';
    var fromFile = Ti.Filesystem.getFile( Ti.Filesystem.resourcesDirectory, myFilename );
    var toFile = Ti.Filesystem.getFile( Ti.Filesystem.applicationDataDirectory, myFilename );
    if ( fromFile.exists() && (!toFile.exists()) ) {
        toFile.write( fromFile.read() );

    The above is just typed into my reply and untested but the syntax should be close. The file object docs suggest that write() can take another file object as an argument, so it may be possible to this simplification:

    toFile.write( fromFile );

    But I have always used the fromFile.read() syntax in my projects so far.

    — answered November 16th 2010 by Doug Handy
  • Vincent

    Have you seen the good example in the KitchenSink demonstrating file manipulation? See filesystem.js.

    The resources directory is read-only, as you say, and so you cannot write to it or place your files in it. However, you may use the applicationDataDirectory constant to write your files to the appdata directory. This is what the API documentation Titanium.Filesystem-module page says about it:

    readonly constant where your application data directory is located.
    this directory should be used to place applications-specific files.
    on iPhone, this directory is also backed up.
    — answered November 16th 2010 by Paul Dowsett
  • Thanks Hal… My question about the readonly resource directory was that: where is it placed in the dir tree. I am assuming its under /RESOURCES.

    My problem was that I was having trouble reading a file from it, but I figured it out. It was the file.

    — answered November 17th 2010 by vincent youmans
  • This is also what I am looking for….

    — answered December 10th 2010 by Peter Lum
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.