你能告诉我如何删除phonegap中的文本文件的争议吗?
我有文本文件,上面有一些文字.我想在插入新文本之前删除该文本.这可能在phonegap中吗?
我能够读取,写入,删除文件.但是如何删除文件的争议?
我有文本文件,上面有一些文字.我想在插入新文本之前删除该文本.这可能在phonegap中吗?
我能够读取,写入,删除文件.但是如何删除文件的争议?
解决方法
你可以使用Truncate.
如果你之后要写,那会更复杂一些.你不能只是
writer.truncate(0); writer.write("Leo was here");
如果你这样做似乎既不起作用,但每个人都单独工作.因此,为了使其工作,您必须等到截断完成后才能执行写操作.在截断的onwriteend中添加写入.注意清除或更改onwwriteend很重要,否则你将获得无限循环.
function clearFile(fileName){ window.requestFileSystem(LocalFileSystem.PERSISTENT,function(fileSystem){ fileSystem.root.getFile(fileName,{ create: false },clearfileExists,fileDoesNotExist); },getFSFail); }
function clearfileExists(fileEntry){ console.log("File " + fileEntry.fullPath + " exists!"); fileEntry.createWriter(truncateFile,fileDoesNotExist); }
现在你有了一个文件编写器.调用truncate(0)并在onwriteend中清除onwrite结束并写下你想要的内容.
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); }
并且为了完整性,这里是错误案例的处理
function fileDoesNotExist(){ console.log("file does not exist"); } function getFSFail(evt) { console.log(evt.target.error.code); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。