How to delete text file contents in phonegap?

Could you tell me how to remove a text file conflict in a telephone conversation? I have a text file with text on it. I want to delete this text before inserting new text. Is this possible in a telephone stumble? I can read, write, delete a file. But how to remove file statements?

+4
source share
2 answers

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); } 
+4
source

You can use the truncate method to delete all content up to a certain amount or if you get rid of everything that can always be deleted from a file and create a new empty version.

 function win(writer) { writer.truncate(0); }; var fail = function(evt) { console.log(error.code); }; file.createWriter(win, fail); 
0
source

Source: https://habr.com/ru/post/1494488/


All Articles