Writing Files in PhoneGap using FileWriter

In the last article we discussed the Flags and the LocalFileSystem in PhoneGap. To write to a file, we require a FileWriter object. A FileWriter is an object that is created for a single file and once created, it can be used to write to a file multiple times. This object maintains the file's position and length attributes, so that you can search and write anywhere in the file. By default, the FileWriter object writes to the beginning of the file and then will overwrite existing data. You can set the optional append Boolean value to true in the FileWriter's constructor to begin writing at the end of the file.

The following are the properties for this object:

  • readyState : This is one of the three states(INIT,WRITING, OR DONE) the reader can be in.
  • fileName : This specifies the name of the file to be Written.
  • length : This specifies the length of the file to be written.
  • position : This specifies the current position of the file pointer.
  • error : This specifies the object containing error.
  • onwritestart : This is called when the write start.
  • onwrite : This is called when the request has been successfully completed.
  • onabort : This is called when the write has been aborted.
  • onerror : This is called when the write has failed.
  • onwriteend : This is called whenthe request has completed (either in success or failure).

The FileWriter object has the following methods:

  • seek : This is used to move the file pointer to the byte specified.
  • truncate : This is used to shorten the file to the length specified.
  • write : This is used to write data to the file.
  • abort : This is used to abort the writing of a file.

Example for seek() method

function success(writer) {
            //forwards file pointer to end of file
            writer.seek(writer.length);
        };

        var failed = function (evt) {
            console.log(error.code);
        };
        entry.createWriter(success, failed);

Example for truncate() method

function success(writer) {
            writer.truncate(10);
        };
        var failed = function (evt) {
            console.log(error.code);
        };
       entry.createWriter(success,failed);

Example for write() method

function success(writer) {
            writer.onwrite = function (evt) {
                console.log("write success");
            };
            writer.write("some sample text");
        };
        var failed = function (evt) {
            console.log(error.code);
        };
        entry.createWriter(success,failed);

Example for abort() method

function success(writer) {
             writer.onwrite = function (evt) {
                 console.log("write success");
             };
             writer.write("some sample text");
             writer.abort();
         };
         var failed = function (evt) {
             console.log(error.code);
         };
         entry.createWriter(success, failed);

Supported Platform

  • Android
  • Blackberry WebWorks (OS 5.0 and higher)
  • iOS
  • Windows Phone 7 (Mango)

Resources

Some of the useful resources are as follows.

Filesystem-Reading Files in PhonGap
Using Flags and LocalFileSystem in PhoneGap
Working With Networks in Windows Phone 7 Using PhoneGap
Media in PhoneGap

Up Next
    Ebook Download
    View all
    Learn
    View all