Search code examples
androidfileserializationobjectinputstreamobjectoutputstream

java.io.StreamCorruptedException: Wrong format: ac error android


I am writing serialized objects to file and reading after it in android. I am getting " java.io.StreamCorruptedException: Wrong format: ac " error while reading second object from file. Below is the code for writing object to file.

public void writeToBinary (String filename, Object obj, boolean append){
    file = new File ("mySchedule");

    try{
        if (!append) { out = new ObjectOutputStream (getContext().openFileOutput(filename,Context.MODE_PRIVATE)); }
        else { out = new AppendableObjectOutputStream (getContext().openFileOutput(filename, Context.MODE_APPEND)); }
     //   Toast.makeText(getActivity().getApplicationContext(), "Starting to add to File", Toast.LENGTH_SHORT).show();
        out.writeObject(obj);
        out.flush();
        Toast.makeText(getActivity().getApplicationContext(), "Added to File", Toast.LENGTH_SHORT).show();
    }catch (Exception e){
        e.printStackTrace ();
       // Toast.makeText(getActivity().getApplicationContext(), "Exception to File", Toast.LENGTH_SHORT).show();
    }finally{
        try{
            if (out != null) out.close ();
          //  Toast.makeText(getActivity().getApplicationContext(), "Close to File", Toast.LENGTH_SHORT).show();
        }catch (Exception e){
            e.printStackTrace ();
        }
    }
}

private static class AppendableObjectOutputStream extends ObjectOutputStream {
    public AppendableObjectOutputStream(OutputStream out) throws IOException {
        super(out);
    }
    @Override
    protected void writeStreamHeader() throws IOException {}
}

I am calling the method using below line of code,every time user clicks on button and writing the object to file.Method will be called and object will be saved as many times as the user clicks on button.

       if(MainActivity.firstTime) {
            writeToBinary("mySchedule", scheduleBeans, false);
            MainActivity.firstTime=false;
        } else {
                writeToBinary("mySchedule", scheduleBeans, true);
        }

Here,scheduleBeans is the object of the class which implements serializable. MainActivity.firstTime is the public static boolean variable defined in the activity and used as a flag.

Now I am reading the file using below code.

    ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(getContext().openFileInput("mySchedule"));
           Toast.makeText(getActivity().getApplicationContext(), "File Opened", Toast.LENGTH_SHORT).show();
            while (true) {
                arrayList.add((ScheduleBeans) ois.readObject());
                Toast.makeText(getActivity().getApplicationContext(), "Read from File", Toast.LENGTH_SHORT).show();
            }

        } catch (EOFException e) {
            Toast.makeText(getActivity().getApplicationContext(), "Exception End of File", Toast.LENGTH_SHORT).show();

        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getActivity().getApplicationContext(), "Exception to Opening File", Toast.LENGTH_SHORT).show();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                 //   Toast.makeText(getActivity().getApplicationContext(), "Closing file", Toast.LENGTH_SHORT).show();
                }
            } catch (IOException e) {
                e.printStackTrace();
               // Toast.makeText(getActivity().getApplicationContext(), "Exception in Closing file", Toast.LENGTH_SHORT).show();
            }
        }

I am getting the error "java.io.StreamCorruptedException: Wrong format: ac" while reading the second object from file in case file has more than one objects in it.

Can you please tell what should be the change in code to read all the objects in file successfully?

Update-- Issue resolved. Above code in question is edited to the solved working code.


Solution

  • You're using MODE_APPEND in both cases, including the case when you're not supposed to be appending to the file.

    You should only use it in the other case.