在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);
很抱歉第一个答案不正确。显然你不能通过 FileEntry到FileWriter构造函数你需要传递 File这是通过调用 FileEntry.file() 获得的方法。
我一定喜欢这个复杂的 W3C 规范。
关于javascript - 应用程序重新启动后追加到文件 - Phonegap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10448794/