phonegap filewriter附加问题

时间:2011-07-29 14:07:52

标签: android cordova

尝试使用phonegap 0.9.6文件存储。我编写文件时代码工作正常,但是当我尝试追加到文件时(使用搜索,截断等)不起作用。

版本0.9.5中存在一个似乎已修复的错误。

当我调用writer.seek时,代码才会终止。截断后的警报(或者如果我删除了截断搜索)则根本没有被调用。

我应该在某处设置追加标志吗?该文档说,但没有举例说明我应该在哪里设置追加标志。代码如下

function gotFS(fileSystem) {
        fileSystem.root.getFile( "test.txt", {"create":true,
            "exclusive":false}, gotFileEntry, fail);
    }

    function gotFileEntry(fileEntry) {
        fileEntry.createWriter(gotFileWriter, fail);
    }

    function gotFileWriter(writer) {
        writer.onwrite = function(evt) {
            console.log("write success");
        };

        writer.write("some sample text");
        // contents of file now 'some sample text'
        writer.truncate(11);
        alert('truncated');
        // contents of file now 'some sample'
        writer.seek(writer.length); //writer.seek(4) does not work either

        // contents of file still 'some sample' but file pointer is after the 'e' in 'some'
        writer.write(" different text");
        // contents of file now 'some different text'
        alert('success with diff text');
    }

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

我找到了解决方法:

        function gotFileWriter(writer) {
            writer.onwrite = function(evt) {
                console.log("write success");
            };

            writer.write("some sample text");
            // contents of file now 'some sample text'
            writer.abort();
            writer.truncate(11);
            // contents of file now 'some sample'
            writer.abort();
            writer.seek(writer.length); //writer.seek(4) does not work either

            // contents of file still 'some sample' but file pointer is after the 'e' in 'some'
            writer.write(" different text");
        }

答案 1 :(得分:0)

文件处理是异步的。您不能简单地逐步执行文件操作。在进行下一步之前,您必须等待onwrite事件。 writer.abort()可以修复错误。但你无法确定存储的是什么。