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:

The FileWriter object has the following methods:

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

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