Titanium.Filesystem.File
> Titanium.Filesystem.File

Object representing a path to a file or directory in the device's persistent storage.

Use the Titanium.Filesystem.getFile method to get a handle to a File object, which represents a given path. There does not need to be an existing file or directory does not need to exist before getFile is called. If the file doesn't exist, and the file path identifies a file in a writable directory, writing to the file creates the file implicitly.

See Titanium.Filesystem for constants identifying commonly-used device directories.

Use the exists method to test whether the file exists.

A file object can point to an ordinary file, a directory or a symbolic link. Use createDirectory to create a directory. Use the getDirectoryListing method to retrieve a list of the directory's contents.

The File object doesn't provide methods for random access into the file. The read method reads the file's entire contents into a Blob object. The write method can either overwrite the entire file or append to an existing file.

For random access to a file, such as accessing a small portion of a larger file, you can open a file as a FileStream object. Use the open method to get a FileStream for an existing File object, or use the Titanium.Filesystem.openStream method to get a FileStream directly without calling getFile first.

The Titanium.Filesystem module defines a number of properties and methods related to filesystem access, including properties that specify paths for application-specific directories, and methods for creating temporary files and directories.

On Android, files may be stored on external storage (that is, removable media such as SD Cards).

Note that once created with getFile, the path associated with a file object is immutable. If you move the underlying file using move or rename, you can no longer access it with the original File object. You must use getFile to get a handle to the new path.

Resource Files

The Resources directory and all the files in it are read-only. On Android, resource files are stored in the resource bundle and do not have all of the properties of normal files. In particular, they do not have creation or modification timestamps.

Examples

Reading a File

Data files shipped with the application are stored in the resources directory.

This example reads string data from a text file.

// resourcesDirectory is actually the default location, so the first
// argument could be omitted here.
file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, "textfile.txt");
var blob = file.read();
var readText = blob.text;
// dispose of file handle & blob.
file = null;
blob = null;

Creating a Subdirectory

Files that the application writes to need to be stored outside of the resources directory, since that directory is read-only.

This example creates a subdirectory to store downloaded images. The example assumes that two variables are defined elsewhere in the code: myImageID, a string containing some kind of ID for the downloaded image, and myImageData, a Blob containing JPEG image data.

var imageDir = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,
    'downloaded_images');
if (! imageDir.exists()) {
    imageDir.createDirectory();
}

// .resolve() provides the resolved native path for the directory.
var imageFile  = Ti.Filesystem.getFile(imageDir.resolve(), myImageID + '.jpg');
Ti.API.info("ImageFile path is: " + imageFile.resolve());
if (imageFile.write(myImageData)===false) {
    // handle write error
}
// dispose of file handles
imageFile = null;
imageDir = null;
  • 0.8
  • 0.8
  • 0.8
Defined By

Properties

apiName : Stringreadonly

The name of the API that this proxy corresponds to.

The name of the API that this proxy corresponds to.

The value of this property is the fully qualified name of the API. For example, Button returns Ti.UI.Button.

  • 3.2.0
  • 3.2.0
  • 3.2.0
Indicates if the proxy will bubble an event to its parent. ...

Indicates if the proxy will bubble an event to its parent.

Some proxies (most commonly views) have a relationship to other proxies, often established by the add() method. For example, for a button added to a window, a click event on the button would bubble up to the window. Other common parents are table sections to their rows, table views to their sections, and scrollable views to their views. Set this property to false to disable the bubbling to the proxy's parent.

Default: true

  • 3.0.0
  • 3.0.0
  • 3.0.0
Titanium.Filesystem.File
executable : Booleanreadonly

true if the file is executable.

true if the file is executable.

On iOS, this property exists but is always false.

Titanium.Filesystem.File
hidden : Boolean

Set to true if the file is hidden.

Set to true if the file is hidden.

On iOS, this property exists but is always false.

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

If this property is set to a Window or TabGroup, then the corresponding Activity lifecycle event callbacks will also be called on the proxy. Proxies that require the activity lifecycle will need this property set to the appropriate containing Window or TabGroup.

  • 3.6.0
Titanium.Filesystem.File
name : Stringreadonly

Name of the file.

Name of the file.

Titanium.Filesystem.File
nativePath : Stringreadonly

Native path associated with this file object, as a file URL.

Native path associated with this file object, as a file URL.

On iOS, use the resolve method to obtain a plain file path for use with native modules.

Titanium.Filesystem.File
parent : Titanium.Filesystem.Filereadonly

A File object representing the parent directory of the file identified by this object.

A File object representing the parent directory of the file identified by this object.

iOS platform-note: Prior to Titanium SDK 7.0.0, this method returned the path of the parent directory as a String. Since Titanium SDK 7.0.0 it returnes a Titanium.Filesystem.File reference for parity wih Android and Windows.

  • 0.8
Titanium.Filesystem.File
readonly : Booleanreadonly

true if the file identified by this object is read-only.

true if the file identified by this object is read-only.

  • 0.8
Titanium.Filesystem.File
: Boolean
Value indicating whether or not to back up to a cloud service. ...

Value indicating whether or not to back up to a cloud service.

Some apps may be rejected by Apple for backing up specific files; if this is the case, ensure that this value is set to false for them. This value should only need to be set once by your app, but setting it multiple times will not cause problems. For files distributed with your app, this will need to be set on boot. This flag will only affect iOS versions 5.0.1 and later, but is safe to set on earlier versions.

Note that setting this property to false will also prevent the file identified by this object from being backed up to iTunes.

Default: true

  • 1.8.0
  • 1.8.0
Titanium.Filesystem.File
size : Numberreadonly

Size, in bytes, of the file identified by this object.

Size, in bytes, of the file identified by this object.

Titanium.Filesystem.File
writable : Booleanreadonly

true if the file identified by this object is writable.

true if the file identified by this object is writable.

Titanium.Filesystem.File
writeable : Booleanreadonlyremoved

true if the file identified by this object is writable.

true if the file identified by this object is writable.

This property has been removed since 6.0.0

Use <Titanium.Filesystem.File.writable> instead.

  • 0.8
  • 0.8
Defined By

Methods

Adds the specified callback as an event listener for the named event. ...

Adds the specified callback as an event listener for the named event.

Parameters

  • name : String

    Name of the event.

  • callback : Callback<Object>

    Callback function to invoke when the event is fired.

Returns

  • void
Titanium.Filesystem.File
( data ) : Boolean
Appends data to the file identified by this file object. ...

Appends data to the file identified by this file object.

Data to append can be specified as a String, Blob, or File.

If the data argument is a File object, the file's contents are appended to this file.

Returns true if the operation succeeds.

Parameters

Returns

  • Boolean
Applies the properties to the proxy. ...

Applies the properties to the proxy.

Properties are supplied as a dictionary. Each key-value pair in the object is applied to the proxy such that myproxy[key] = value.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • props : Dictionary

    A dictionary of properties to apply.

Returns

  • void
Titanium.Filesystem.File
( destinationPath ) : Boolean
Copies the file identified by this file object to a new path. ...

Copies the file identified by this file object to a new path.

Returns true if the copy succeeds.

  • 0.8

Parameters

  • destinationPath : String

    Destination path to copy to.

Returns

  • Boolean
Titanium.Filesystem.File
( ) : Boolean
Creates a directory at the path identified by this file object. ...

Creates a directory at the path identified by this file object.

Returns true if the directory was created successfully.

Returns

  • Boolean
Titanium.Filesystem.File
( ) : Boolean
Creates a file at the path identified by this file object. ...

Creates a file at the path identified by this file object.

Note that if you write to a file that doesn't exist, the file is created automatically, so it is not necessary to call this method unless you want to explicitly create the file (for example, to create an empty file).

Returns true if the file was created successfully. Returns false if the file already exists, or if the file couldn't be created for some other reason.

  • 6.1.0
  • 0.9.0
  • 0.9.0

Returns

  • Boolean
Titanium.Filesystem.File
( ) : Number
Returns the creation timestamp for the file identified by this file object. ...

Returns the creation timestamp for the file identified by this file object.

On Android, returns 0 for resource files.

Returns

  • Number
Titanium.Filesystem.File
( [recursive] ) : Boolean
Deletes the directory identified by this file object. ...

Deletes the directory identified by this file object.

Returns true if the operation was successful. Does nothing if the file object does not identify a directory.

Parameters

  • recursive : Boolean (optional)

    Pass true to recursively delete any directory contents.

Returns

  • Boolean
Titanium.Filesystem.File
( ) : Boolean
Deletes the file identified by this file object. ...

Deletes the file identified by this file object.

Returns true if the operation was successful.

Returns

  • Boolean
Titanium.Filesystem.File
( ) : Boolean
Returns true if the file or directory identified by this file object exists on the device. ...

Returns true if the file or directory identified by this file object exists on the device.

Returns

  • Boolean
Titanium.Filesystem.File
( ) : String
Returns the extension for the file identified by this file object. ...

Returns the extension for the file identified by this file object.

Returns

  • String
Fires a synthesized event to any registered listeners. ...

Fires a synthesized event to any registered listeners.

Parameters

  • name : String

    Name of the event.

  • event : Dictionary

    A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.

Returns

  • void
Gets the value of the apiName property. ...

Gets the value of the apiName property.

  • 3.2.0
  • 3.2.0
  • 3.2.0

Returns

  • String
Gets the value of the bubbleParent property. ...

Gets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Returns

  • Boolean
Titanium.Filesystem.File
( ) : String[]
Returns a listing of the directory identified by this file object, or null if this object doesn't identify a directory. ...

Returns a listing of the directory identified by this file object, or null if this object doesn't identify a directory.

Returns

  • String[]
Titanium.Filesystem.File
( ) : Boolean
Gets the value of the executable property. ...

Gets the value of the executable property.

Returns

  • Boolean
Titanium.Filesystem.File
( ) : Boolean
Gets the value of the hidden property. ...

Gets the value of the hidden property.

Returns

  • Boolean
Gets the value of the lifecycleContainer property. ...

Gets the value of the lifecycleContainer property.

  • 3.6.0

Returns

Titanium.Filesystem.File
( ) : String
Gets the value of the name property. ...

Gets the value of the name property.

Returns

  • String
Titanium.Filesystem.File
( ) : String
Gets the value of the nativePath property. ...

Gets the value of the nativePath property.

Returns

  • String
Titanium.Filesystem.File
( ) : Titanium.Filesystem.File
Gets the value of the parent property. ...

Gets the value of the parent property.

  • 0.8

Returns

Titanium.Filesystem.File
( ) : String
Returns the protection key value of this file object. ...

Returns the protection key value of this file object. Returns null if there's an error.

  • 4.1.0
  • 4.1.0

Returns

Titanium.Filesystem.File
( ) : Boolean
Gets the value of the readonly property. ...

Gets the value of the readonly property.

  • 0.8

Returns

  • Boolean
Titanium.Filesystem.File
( ) : Boolean
Gets the value of the remoteBackup property. ...

Gets the value of the remoteBackup property.

  • 1.8.0
  • 1.8.0

Returns

  • Boolean
Titanium.Filesystem.File
( ) : Number
Gets the value of the size property. ...

Gets the value of the size property.

Returns

  • Number
Titanium.Filesystem.File
( ) : Boolean
Gets the value of the writable property. ...

Gets the value of the writable property.

Returns

  • Boolean
Titanium.Filesystem.File
( ) : Booleanremoved
Gets the value of the writeable property. ...

Gets the value of the writeable property.

This method has been removed since 6.0.0

Use <Titanium.Filesystem.File.writable> instead.

  • 0.8
  • 0.8

Returns

  • Boolean
Titanium.Filesystem.File
( ) : Boolean
Returns true if this file object represents a directory. ...

Returns true if this file object represents a directory.

  • 0.8
  • 3.1.0
  • 3.1.0

Returns

  • Boolean
Titanium.Filesystem.File
( ) : Boolean
Returns true if this file object represents an ordinary file. ...

Returns true if this file object represents an ordinary file.

  • 0.8
  • 3.1.0
  • 3.1.0

Returns

  • Boolean
Titanium.Filesystem.File
( ) : Number
Returns the last modification time for this file. ...

Returns the last modification time for this file.

On Android, returns 0 for resource files.

Returns

  • Number
Titanium.Filesystem.File
( newpath ) : Boolean
Moves the file identified by this file object to another path. ...

Moves the file identified by this file object to another path.

Note that this method moves the stored file, but doesn't update this file object to point to the new path. To access the file after moving it, you must call getFile using the destination path to obtain a new file handle.

Parameters

  • newpath : String

    New location for the file.

Returns

  • Boolean
Titanium.Filesystem.File
( mode ) : Titanium.Filesystem.FileStream
Opens the file identified by this file object for random access. ...

Opens the file identified by this file object for random access.

You can open the file for reading, writing, or appending by specifying one of the MODE constants from Titanium.Filesystem: MODE_READ, MODE_WRITE, or MODE_APPEND.

The FileStream object returned by this call can be used to read from, write to, or append to the file, depending on what mode the file is opened in.

Parameters

  • mode : Number

    Mode to open the file in: MODE_READ, MODE_WRITE, or MODE_APPEND.

Returns

Titanium.Filesystem.File
( ) : Titanium.Blob
Returns the contents of the file identified by this file object as a Blob. ...

Returns the contents of the file identified by this file object as a Blob.

Returns

Removes the specified callback as an event listener for the named event. ...

Removes the specified callback as an event listener for the named event.

Multiple listeners can be registered for the same event, so the callback parameter is used to determine which listener to remove.

When adding a listener, you must save a reference to the callback function in order to remove the listener later:

var listener = function() { Ti.API.info("Event listener called."); }
window.addEventListener('click', listener);

To remove the listener, pass in a reference to the callback function:

window.removeEventListener('click', listener);

Parameters

  • name : String

    Name of the event.

  • callback : Callback<Object>

    Callback function to remove. Must be the same function passed to addEventListener.

Returns

  • void
Titanium.Filesystem.File
( newname ) : Boolean
Renames the file identified by this file object. ...

Renames the file identified by this file object.

Returns true if the file was successfully renamed.

Fails if the destination is in a different directory than the current file. Use move to move a file to a different directory.

Note that this method renames the stored file, but doesn't update this file object to point to the new path. To access the file after renaming it, you must call getFile using the destination path to obtain a new file handle.

Parameters

  • newname : String

    New name for the file.

Returns

  • Boolean
Titanium.Filesystem.File
( ) : String
Returns the fully-resolved native path associated with this file object. ...

Returns the fully-resolved native path associated with this file object.

On iOS, the path returned by this method is a plain file path, not a URL. It is suitable for use in native modules that need to access the file using native APIs.

On Android, the return value of resolve is a file:// URL, identical to the nativePath property.

Returns

  • String
Sets the value of the bubbleParent property. ...

Sets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • bubbleParent : Boolean

    New value for the property.

Returns

  • void
Titanium.Filesystem.File
( hidden )
Sets the value of the hidden property. ...

Sets the value of the hidden property.

Parameters

  • hidden : Boolean

    New value for the property.

Returns

  • void
Sets the value of the lifecycleContainer property. ...

Sets the value of the lifecycleContainer property.

  • 3.6.0

Parameters

Returns

  • void
Titanium.Filesystem.File
( fileProtectionType ) : Boolean
Sets the protection key as an attribute to the file identified by this file object. ...

Sets the protection key as an attribute to the file identified by this file object.

Returns true if successfully set. Returns false if failed.

  • 4.1.0
  • 4.1.0

Parameters

Returns

  • Boolean
Titanium.Filesystem.File
( remoteBackup )
Sets the value of the remoteBackup property. ...

Sets the value of the remoteBackup property.

  • 1.8.0
  • 1.8.0

Parameters

  • remoteBackup : Boolean

    New value for the property.

Returns

  • void
Titanium.Filesystem.File
( ) : Number
Returns the amount of free space available on the device where the file identified by this file object is stored. ...

Returns the amount of free space available on the device where the file identified by this file object is stored.

Free space is returned in bytes.

Returns

  • Number
Titanium.Filesystem.File
( data, [append] ) : Boolean
Writes the specified data to the file identified by this file object. ...

Writes the specified data to the file identified by this file object.

If the append parameter is false or not specified, any existing data in the file is overwritten.

If append is true, the specified data is appended to the end of the file.

Parameters

Returns

  • Boolean