Using File Objects

Jaxer allows access to individual files on the file system through the Jaxer.File class.

Instantiating a File object

We can instantiate a file object by simply newing up Jaxer.File(pathToFile). Note that creating a file object does not automatically create the equivalent object on the file system.

The Jaxer.File object will be created with 'resolved' paths, which require a full path to the referenced file system object. The following code will throw an error 'NS_ERROR_FILE_UNRECOGNIZED_PATH': To deal with this issue, use the provided Jaxer.Dir.resolvePath(path) method which will create the required path.

This will give us a JavaScript object called myFile which provides many useful methods for accessing the file on the file system.

Getting information about the file

Now that we have a file object, we can retrieve a lot of useful information about the file by simply using the provided accessor methods. The API docs contain a full description for the file objects

The most useful (or at least most commonly used) method for file objects is exists(). This provides us with a boolean value predicated on the existance of the referenced file on the file system.

Now that we have an instantiated file object called 'myFile', we can interrogate it to get useful information about the file and its properties.

Managing files on the filesystem

Some methods are available to directly manage the files.

Creating a file

To create a file we invoke the create() method on a file object.

Deleting a file

To delete a file we use the remove() method.

Once you delete the file, some methods previously available on the file object will throw exceptions if accessed.

Truncating a file

To truncate a file (i.e., to empty the file), use the truncate() method.

After truncating, the file size will be reported as 0 bytes.

Copying a file

To copy the file, use the copy() method, which will copy the file to a provided file location.

The copy() method will throw an exception if the destination file already exists.