title: Write an ArrayList to SD-Card
body: <p>When my application starts it connects to a database and downloads some data in XML format. This is the layout: </p>
  
  <pre><code>&lt;Customers&gt;
    &lt;Customer&gt; 
      &lt;name&gt;...&lt;/name&gt;
      ...
    &lt;/Customer&gt;
     &lt;Customer&gt; 
      &lt;name&gt;...&lt;/name&gt;
      ...
    &lt;/Customer&gt;
    ...
  &lt;/Customer&gt;
  </code></pre>
  
  <p>For each Customer I create an Object of the class <code>Customer</code> wicht I store in a ArrayList. The users can edit and add some data of each Customer and they can upload it back to the server. </p>
  
  <p>The problem is that I don't know what's the best way to store the data locally, so if my application closes or the user don't have internet anymore they'll have a backup. I now covert all <code>Customer</code> objects back to XML and then store it on the SD Card, but this isn't a good solution I think. Each customer has about 40 String, 10 integers and a few booleans I have to store. Is there a better way to store the data locally? </p>
  
  <p>I found <a href="https://github.com/blcooley/ArrayListTest/blob/master/src/com/coolmodev/arraylisttest/MainActivity.java" rel="nofollow">some code</a> wich can store an ArrayList but it does not work with custom classes. </p>
  
  <p><strong>Edit:</strong> I used <a href="http://www.jondev.net/articles/Android_Serialization_Example_%28Java%29" rel="nofollow">this tutorial</a> to fix my problem.</p>
  
  <pre><code>import java.io.BufferedInputStream;
  import java.io.BufferedOutputStream;
  import java.io.ByteArrayInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutput;
  import java.io.ObjectOutputStream;
  import java.util.ArrayList;
  
  import android.os.Environment;
  
  public class SerializeData {
      public static byte[] serializeObject(Object o) {
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
  
          try {
              ObjectOutput out = new ObjectOutputStream(bos);
              out.writeObject(o);
              out.close();
  
              // Get the bytes of the serialized object
              byte[] buf = bos.toByteArray();
  
              return buf;
          } catch (IOException ioe) {
              CreateLog.addToLog(ioe.toString());
              return null;
          }
      }
  
      public static Object deserializeObject(byte[] b) {
          try {
              ObjectInputStream in = new ObjectInputStream(
                      new ByteArrayInputStream(b));
              Object object = in.readObject();
              in.close();
  
              return object;
          } catch (ClassNotFoundException cnfe) {
              CreateLog.addToLog(cnfe.toString());
  
              return null;
          } catch (IOException ioe) {
              CreateLog.addToLog(ioe.toString());
  
              return null;
          }
      }
  
      public static void saveData(){
          byte[] arrayData = SerializeData
                  .serializeObject(MyTasks.allCustomers);
  
          BufferedOutputStream bos;
          try {
              bos = new BufferedOutputStream(new FileOutputStream(
                      Environment.getExternalStorageDirectory()
                              + "/myprogram/myarray.txt"));
              bos.write(arrayData);
              bos.flush();
              bos.close();
          } catch (FileNotFoundException e) {
              CreateLog.addToLog(e.toString());
          } catch (IOException e) {
              CreateLog.addToLog(e.toString());
          }
      }
  
      public static ArrayList&lt;Customer&gt; getData(){
          File afile = new File(Environment.getExternalStorageDirectory()
                  + "/myprogram/myarray.txt");
          int size = (int) afile.length();
          byte[] bytes = new byte[size];
          try {
              BufferedInputStream buf = new BufferedInputStream(new FileInputStream(afile));
              buf.read(bytes, 0, bytes.length);
              buf.close();
          } catch (FileNotFoundException e) {
              CreateLog.addToLog(e.toString());
          } catch (IOException e) {
              CreateLog.addToLog(e.toString());
          }
  
          return (ArrayList&lt;Customer&gt;) SerializeData.deserializeObject(bytes); 
  
      }
  }
  </code></pre>