如何删除phonegap中的文本文件的内容? [英] How to delete contents of text file in phonegap?

查看:142
本文介绍了如何删除phonegap中的文本文件的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能告诉我如何删除在phonegap文本文件的竞争?
我有一个文本文件,它有一些文本。我想删除该文本,然后再插入新文本。这是可能在phonegap吗?

Can you please tell me how to delete the contend of text file in phonegap? I have text file having some text on it .I want to remove that text before inserting the new text .Is this possible in phonegap? I am able to read , write , delete file .But how to remove the contends of file?

推荐答案

你可以读取,写入,删除文件。可以使用Truncate。

You can use Truncate.

如果你之后要写,这只是一个更复杂。您不能只是

It is just a little more complicated if you are going to write afterwards. You can't just

writer.truncate(0);
writer.write("Leo was here");

如果你这样做,似乎都不工作,但每个单独工作。所以要使它工作你必须等待截断完成之前做写。在truncate的onwriteend中添加写入。 NB重要的是清除或更改onwwriteend,否则会得到一个无限循环。

If you do that neither seems to work but each one works individually. So to make it work you have to wait until the truncate finishes before doing the write. Add the write in the onwriteend of the truncate. NB It is important to clear or change the onwwriteend otherwise you will get an infinite loop.

因此,首先获取文件系统并使用文件系统获取文件条目



So start by getting the filesystem and use the filesystem 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 got a file entry (the file exists) then create the filewriter.

function clearfileExists(fileEntry){
    console.log("File " + fileEntry.fullPath + " exists!");
    fileEntry.createWriter(truncateFile, fileDoesNotExist);
}

现在你有一个文件写入器。调用truncate(0),并在onwriteend中清除onwrite结束并写入所需的内容。

Now that you have a file writer. call truncate(0) and in the onwriteend clear the onwrite end and write 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 here is the handling of the error cases

function fileDoesNotExist(){
    console.log("file does not exist");
}
function getFSFail(evt) {
    console.log(evt.target.error.code);
}

这篇关于如何删除phonegap中的文本文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆