2011-07-29 10 views
3

phonegap 0.9.6ファイルストレージを使用しようとしています。私はファイルを書いている間、コードは正常に動作しますが、シークや切り捨てなどを使ってファイルに追加しようとすると動作しません。phonegap filewriter append issue

バージョン0.9.5にバグが修正されたようです。

writer.seekを呼び出すとコードが終了します。切り捨て後のアラート(または切り詰めを削除した場合)はまったく呼び出されていません。

どこかで追加フラグを設定する必要がありますか?ドキュメントによれば、appendフラグをどこに設定すべきかの例は示されていません。コードは次のとおりです

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'); 
    } 

助けていただければ幸いです。

答えて

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"); 
     } 
+0

私は最初の行だけを印刷し、すべてのアプリケーション再起動時にループを実行するようにしたい場合は、1行追加します.http://stackoverflow.com/questions/16192825/failed-to- write-10-lines-to-a-file-using-loop-in-javascript-phonegap – user366584

+0

私はイベントハンドラを利用して次の方法をお勧めします:http://stackoverflow.com/a/23836103/925861 – Stradivari

0

ファイル処理がasynchronです。単にファイル操作を段階的に行うことはできません。あなたは、次のステップを行う前に、onwriteイベントを待たなければなりません。 writer.abort()がエラーを修正することがあります。しかし、あなたは何が格納されているか分からない。