FileWriter

FileWriter is an object that allows one to write a file.

Properties

  • readyState: One of the three states the reader can be in INIT, WRITING or DONE.
  • fileName: The name of the file to be written. (DOMString)
  • length: The length of the file to be written. (long)
  • position: The current position of the file pointer. (long)
  • error: An object containing errors. (FileError)
  • onwritestart: Called when the write starts. . (Function)
  • onprogress: Called while writing the file, reports progress (progess.loaded/progress.total). (Function) -NOT SUPPORTED
  • onabort: Called when the write has been aborted. For instance, by invoking the abort() method. (Function)
  • onerror: Called when the write has failed. (Function)
  • onwriteend: Called when the request has completed (either in success or failure). (Function)

Methods

  • abort: Aborts writing file.
  • seek: Moves the file pointer to the byte specified.
  • truncate: Shortens the file to the length specified.
  • write: Writes data to the file.

Details

The FileWriter object is a way to write files from the devices file system. Users register their own event listeners to receive the writestart, progress, write, writeend, error and abort events.

A FileWriter is created for a single file. You can use it to write to a file multiple times. The FileWriter maintains the file‘s position and length attributes, so you can seek and write anywhere in the file. By default, the FileWriter writes to the beginning of the file (will overwrite existing data). Set the optional append boolean to true in the FileWriter’s constructor to begin writing to the end of the file.

Supported Platforms

  • Android
  • BlackBerry WebWorks (OS 5.0 and higher)
  • iOS

Seek Quick Example

var paths = navigator.fileMgr.getRootPaths();
var writer = new FileWriter(paths[0] + "write.txt");
// fast forwards file pointer to end of file
writer.seek(writer.length);	

Truncate Quick Example

var paths = navigator.fileMgr.getRootPaths();
var writer = new FileWriter(paths[0] + "write.txt");
writer.truncate(10);	

Write Quick Example

var writeSuccess = function(evt) {
	console.log("Write has succeeded");
};

var paths = navigator.fileMgr.getRootPaths();
var writer = new FileWriter(paths[0] + "write.txt");
writer.onwrite = writeSuccess;
writer.write("some sample text");

Append Quick Example

var writeSuccess = function(evt) {
	console.log("Write has succeeded");
};

var paths = navigator.fileMgr.getRootPaths();
var writer = new FileWriter(paths[0] + "write.txt", true);
writer.onwrite = writeSuccess;
writer.write("some more text");

Abort Quick Example

var aborted = function(evt) {
	console.log(evt.target.error);
};

var paths = navigator.fileMgr.getRootPaths();
var writer = new FileWriter(paths[0] + "write.txt");
writer.onabort = aborted;
writer.write("some sample text");
writer.abort();

Full Example

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                      "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Contact Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for PhoneGap to load
    //
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }

    // PhoneGap is ready
    //
    function onDeviceReady() {
		var paths = navigator.fileMgr.getRootPaths();
		var writer = new FileWriter(paths[0] + "write.txt");
		writer.onwrite = writeSuccess;
		writer.write("some sample text");
		// The file is now 'some sample text'
    }

	function writeSuccess() {
		console.log("Write has succeeded");
		var paths = navigator.fileMgr.getRootPaths();
		var writer = new FileWriter(paths[0] + "write.txt");
		writer.seek(4);
		writer.truncate(writer.position);
		// The file is now 'some'
	}
	
	function fail(evt) {
		console.log(evt.target.error.code);
	}
	
    </script>
  </head>
  <body onload="onLoad()">
    <h1>Example</h1>
    <p>Write File</p>
  </body>
</html>