简体   繁体   中英

Reading and writing objects to android file (ObjectOutput/InputStream)

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:

public Vector<Paciente> listaPacientes (int cantidad){
    Vector<Paciente> result= new Vector<Paciente>();
    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<cantidad && pac!=null);
        f.close();
    }catch(Exception e){
        Log.e("Historial Clinico", e.getMessage(),e);
    }
    return result;
}

And the writing function:

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

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:

public class AppendingObject extends ObjectOutputStream{

public AppendingObject(OutputStream out) throws IOException {
    super(out);
  }

  @Override
  protected void writeStreamHeader() throws IOException {
    reset();
  }

Here's the Error:

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)

The EOFException occurs because you got to the end of the file. It's normal. Catch it and break. Your code seems to assume that readObject() returns null at end of file. It doesn't. It returns null if you wrote null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM