javascript - 应用程序重新启动后追加到文件 - Phonegap

标签 javascript file-io cordova

在phonegap中,有一种方法可以在应用程序启动并创建writer时获取文件的长度,以便您可以将writer.seek()添加到该位置并附加到文件。目前,它会在应用程序运行时追加,但每次应用程序重新启动时都会覆盖该文件。以下是我为作者提供的代码。作者是全局创建的。

我使用的是在 Android 2.3.3 上运行的phonegap 1.2.0

var writer = new FileWriter("mnt/sdcard/mydocs/text.txt");


function appendFile(text) {
  try{
    writer.onwrite = appendSuccess;
    writer.onerror = appendFail;
    writer.seek(writer.length);
    writer.write(text);
    }catch(e){
      alert(e);
      }
}

function appendSuccess() {
    alert("Write successful");
  }

  function appendFail(evt) {
    alert("Failed to write to file");
    }
  }

最佳答案

是的,这是一个错误。在最新版本的 PhoneGap 中,该问题已得到修复。但是,我们不再支持:

new FileWriter(pathToFile);

现在你需要做:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(FS) {
    FS.root.getFile("empty.txt", {"create":true, "exclusive":false}, 
        function(fileEntry) {
            fileEntry.createWriter(
                 function(writer) {
                    console.log("Length = " + writer.length);
                    console.log("Position = " + writer.position);
                    writer.seek(writer.length);
                    console.log("Position = " + writer.position);
                 }, fail);
        }, fail);
}, fail);

很抱歉第一个答案不正确。显然你不能通过 FileEntryFileWriter构造函数你需要传递 File这是通过调用 FileEntry.file() 获得的方法。

我一定喜欢这个复杂的 W3C 规范。

关于javascript - 应用程序重新启动后追加到文件 - Phonegap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10448794/

相关文章:

javascript - AWS lambda : test event configuration: Error in JSON event

javascript - vue v-for 对象 Prop

javascript - 同一路由上的多个 get 方法在 express 和 sequelize 中发生冲突

java - 通过优化读取 N 行 - Java

c++ - 从 C++ 中的文件输入

Cordova 错误 : Failed to fetch platform cordova-browser@~5. 0.0

php - 使用 jQuery 在 div 中加载 PHP 页面

c - ZLIB中有fmemopen()吗

javascript - Phonegap Android HTML 不会更新

javascript - PhoneGap后台插件使用