title: Reading and writing objects to android file (ObjectOutput/InputStream)
body: <p>I'm trying to read objects from a file and add them to a Vector, but I keep getting an error and I can't figure out why.
  Here's the reading function:</p>
  
  <pre><code>public Vector&lt;Paciente&gt; listaPacientes (int cantidad){
      Vector&lt;Paciente&gt; result= new Vector&lt;Paciente&gt;();
      try{
          FileInputStream f=context.openFileInput(FICHERO);
          ObjectInputStream is = new ObjectInputStream(f);
          int n=0;
          Paciente pac;
          do{
              pac=(Paciente) is.readObject();
              if(pac!=null){
                  result.add(pac);
                  n++;
                  Log.d("Número n","n="+n);
              }
          }while(n&lt;cantidad &amp;&amp; pac!=null);
          f.close();
      }catch(Exception e){
          Log.e("Historial Clinico", e.getMessage(),e);
      }
      return result;
  }
  </code></pre>
  
  <p>And the writing function:</p>
  
  <pre><code>    public void guardarPacientes(Paciente pac){
      File file=new File(FICHERO);
      if(file.exists()){
          try {
              FileOutputStream fi = context.openFileOutput(FICHERO, Context.MODE_APPEND);
              AppendingObject os=new AppendingObject(fi);
              os.writeObject(os);
              os.flush();
              os.close();
  
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
      else
      {
          try{
              FileOutputStream f=context.openFileOutput(FICHERO, Context.MODE_APPEND);
              ObjectOutputStream os = new ObjectOutputStream(f);
              os.writeObject(pac);
              os.flush();
              os.close();
          }catch (Exception e){
              Log.e("Historial Clinico",e.getMessage(),e);
          }
      }
  }
  </code></pre>
  
  <p>As you can see I instantiate from a different class if the file already exists, to avoid the header.
  So, here's the other class:</p>
  
  <pre><code>public class AppendingObject extends ObjectOutputStream{
  
  public AppendingObject(OutputStream out) throws IOException {
      super(out);
    }
  
    @Override
    protected void writeStreamHeader() throws IOException {
      reset();
    }
  </code></pre>
  
  <p>Here's the Error:</p>
  
  <blockquote>
    <p><code>08-02 19:21:46.365: E/Historial Clinico(32309): Wrong format: ac
    08-02 19:21:46.365: E/Historial Clinico(32309): java.io.StreamCorruptedException: Wrong format: ac
    08-02 19:21:46.365: E/Historial Clinico(32309):     at java.io.ObjectInputStream.corruptStream(ObjectInputStream.java:701)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:814)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2006)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1963)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at com.example.historiaclinica.PacientestoFile.listaPacientes(PacientestoFile.java:60)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at com.example.historiaclinica.cListaPacientes.onCreate(cListaPacientes.java:17)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at android.app.Activity.performCreate(Activity.java:5372)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at android.app.ActivityThread.access$700(ActivityThread.java:165)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at android.os.Handler.dispatchMessage(Handler.java:99)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at android.os.Looper.loop(Looper.java:137)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at android.app.ActivityThread.main(ActivityThread.java:5455)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at java.lang.reflect.Method.invokeNative(Native Method)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at java.lang.reflect.Method.invoke(Method.java:525)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
    08-02 19:21:46.365: E/Historial Clinico(32309):     at dalvik.system.NativeStart.main(Native Method)</code></p>
  </blockquote>