title: Phonegap File API
body: <p>After weeks of research I decided to use Phonegap's File API to update my app. On the Phonegap Google Group I discovered that I have to go through following steps:</p>
  
  <ol>
  <li>load this file using XHR</li>
  <li>write the file to the file system</li>
  <li>check for internet connection</li>
  <li>if yes, download latest file using FileTransfer object.</li>
  <li>if no, use your cached file</li>
  </ol>
  
  <p>I already did step 1, my file is now being loaded in XHR. Now I'm stuck at step 2. I tried the <a href="http://docs.phonegap.com/en/2.5.0/cordova_file_file.md.html#FileWriter" rel="nofollow">filewriter</a>, but without success and the information on Phonegap's File API isn't really clear. Can anyone help me with this?</p>
  
  <p>Here's my XHR script: </p>
  
  <pre><code>&lt;script language="javascript" type="text/javascript"&gt;
  function loadHome() {
      var request = new XMLHttpRequest();
      request.open("GET", "file:///sdcard/Download/home.json", true);
      request.onreadystatechange = function() {//Call a function when the state changes.
      if (request.readyState == 4) {
          if (request.status == 200 || request.status == 0) {
  
              var home = JSON.parse(request.responseText);
              var data = "&lt;table cellspacing='0'&gt;";
              for (i = 0; i &lt; home.length; i++) {
                  data += "&lt;td&gt;";
                  data += "&lt;a href='" + home[i].link + "'/&gt;";
                  data += "&lt;img src='" + home[i].img + "'/&gt;";
                  data += "&lt;div class='dsc'&gt;" + home[i].expo + "&lt;br&gt;&lt;em&gt;";
                  data += home   [i].datum + "&lt;/em&gt;&lt;/div&gt;&lt;/a&gt;&lt;/td&gt;";
              }
              data += "&lt;/table&gt;";
              var twitter = document.getElementById("home2");
              twitter.innerHTML = data;
          }
      }
  }
  console.log("asking for home");
  request.send();
  
  &lt;/script&gt;
  </code></pre>
  
  <p>And here's my first try at the filewriter:</p>
  
  <pre><code>&lt;script type="text/javascript" charset="utf-8"&gt;
  
  document.addEventListener("deviceready", onDeviceReady, false);
  
  function onDeviceReady() {
      window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
  }
  
  function gotFS(fileSystem) {
      fileSystem.root.getFile("file:///sdcard/Download/home.json", {create: true, exclusive: false}, gotFileEntry, fail);
  }
  
  function gotFileEntry(fileEntry) {
      fileEntry.createWriter(gotFileWriter, fail);
  }
  
  function gotFileWriter(writer) {
      writer.onwriteend = function(evt) {
          console.log("contents of file now 'some sample text'");
          writer.truncate(11);  
          writer.onwriteend = function(evt) {
              console.log("contents of file now 'some sample'");
              writer.seek(4);
              writer.write(" different text");
              writer.onwriteend = function(evt){
                  console.log("contents of file now 'some different text'");
              }
          };
      };
      writer.write("some sample text");
  }
  
  function fail(error) {
      console.log(error.code);
  }
  
  &lt;/script&gt;
  </code></pre>