This version of the documentation is outdated!
Click here for the latest released version.
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 (progress.loaded/progress.total). (Function) -NOT SUPPORTED
- onwrite: Called when the request has completed successfully. (Function)
- 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 with a UTF-8 encoding.
Details
The FileWriter
object is a way to write files to the device file system (UTF-8 encoded). 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
- Windows Phone 7 and 8
- Windows 8
Seek Quick Example
function win(writer) {
// fast forwards file pointer to end of file
writer.seek(writer.length);
};
var fail = function(evt) {
console.log(error.code);
};
entry.createWriter(win, fail);
Truncate Quick Example
function win(writer) {
writer.truncate(10);
};
var fail = function(evt) {
console.log(error.code);
};
entry.createWriter(win, fail);
Write Quick Example
function win(writer) {
writer.onwrite = function(evt) {
console.log("write success");
};
writer.write("some sample text");
};
var fail = function(evt) {
console.log(error.code);
};
entry.createWriter(win, fail);
Append Quick Example
function win(writer) {
writer.onwrite = function(evt) {
console.log("write success");
};
writer.seek(writer.length);
writer.write("appended text");
};
var fail = function(evt) {
console.log(error.code);
};
entry.createWriter(win, fail);
Abort Quick Example
function win(writer) {
writer.onwrite = function(evt) {
console.log("write success");
};
writer.write("some sample text");
writer.abort();
};
var fail = function(evt) {
console.log(error.code);
};
entry.createWriter(win, fail);
Full Example
<!DOCTYPE html>
<html>
<head>
<title>FileWriter Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.3.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwriteend = function(evt) {
console.log("contents of file now 'some sample text'");
writer.truncate(11);
writer.onwriteend = function(evt) {
console.log("contents of file now 'some sample'");
writer.seek(4);
writer.write(" different text");
writer.onwriteend = function(evt){
console.log("contents of file now 'some different text'");
}
};
};
writer.write("some sample text");
}
function fail(error) {
console.log(error.code);
}
</script>
</head>
<body>
<h1>Example</h1>
<p>Write File</p>
</body>
</html>