title: Android write file
body: <p>After weeks of not programming I've decided to finish my application. Last time I was not able to do the file writing and reading and now I want to do it. I could perhaps use databases but this seems a lot easier.
  I have found <a href="http://narenst.wordpress.com/2010/01/25/android-file-handling-readwrite/" rel="nofollow noreferrer">this</a> page from where I copy-pasted the code. At least now I am able to check the content of the file on my phone (I am not using any virtual SD cards for the time being, but it would definitely accelerate the testing).
  Problem is with this code that every time I write the file, close my app and then reopen it and write the file again, the content of the file is reset.</p>
  
  <pre><code>File root = Environment.getExternalStorageDirectory();
  File file = new File(root, "tomato50.txt");
  if (assignArr.size() &gt; 0) {
      try {
          if (root.canWrite()) {
              FileWriter filewriter = new FileWriter(file);
              BufferedWriter out = new BufferedWriter(filewriter);
              for (int i = 0; i &lt; assignArr.size(); i++) {
                  out.write(assignArr.get(i) + "\n");
                  Toast.makeText(MainActivity.this,
                          "out: " + assignArr.get(i), Toast.LENGTH_LONG)
                          .show();
              }
              out.close();
          }
      } catch (IOException e) {
          Log.e("TAG", "Could not write file " + e.getMessage());
      }
  }
  </code></pre>
  
  <p>E.g.</p>
  
  <ol>
  <li>I put one item into assignArr.</li>
  <li>The item is written into the file.</li>
  <li>I close the app. AssignArr gets empty as it is a variable.</li>
  <li>I open the app.</li>
  <li>I put an item into assignArr.</li>
  <li>I close the app. AssignArr gets empty as it is a variable.</li>
  <li>I open the file which shows only the last item.</li>
  </ol>