You can use Truncate.
This is a little trickier if you intend to write later. You can't just
writer.truncate(0); writer.write("Leo was here");
If you do this, then it does not work, but each one works individually. To make it work, you need to wait until the cutoff is complete before writing. Add an entry to the onwriteend truncation. NB It is important to clear or change onwwriteend, otherwise you will get an infinite loop.
So, start by getting the file system and use the file system to get the file entry
function clearFile(fileName){ window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){ fileSystem.root.getFile(fileName, { create: false }, clearfileExists, fileDoesNotExist); }, getFSFail); }
assuming you have a file entry (the file exists), then create a script file.
function clearfileExists(fileEntry){ console.log("File " + fileEntry.fullPath + " exists!"); fileEntry.createWriter(truncateFile, fileDoesNotExist); }
You now have a file writer. call truncate (0) and in onwriteend, clear the end of onwrite and write down what you want.
function truncateFile(writer){ console.log("truncate"); writer.onwriteend= function(evt) { LOG("write"); writer.seek(0); writer.onwriteend = function(evt){ console.log("contents of file now 'Leo was Here'"); } writer.write("Leo was Here"); } writer.truncate(0); }
and for completeness, error cases are considered here
function fileDoesNotExist(){ console.log("file does not exist"); } function getFSFail(evt) { console.log(evt.target.error.code); }