0

I want to implement object persistence in java. I want to write objects one after the other into a file, so that updating objects in this file is akin to adding new rows in a database table.

I am using four classes viz. Main - To write objects into the file, Employee - An Employee class, DeserializeDemo - To deserialise objects from a .ser file on disc and AppendingObjectOutputStream - to override the writeStreamHeader() method.

Attached are the aforementioned classes

package com.company;
import java.io.*;


public class Main implements Serializable {

    public static void main(String [] args) {
        Employee e = new Employee();
        e.name = "ADS";
        e.address = "Qwerty";
        e.SSN = 1856;
        e.number = 91246;

        try {
            FileOutputStream fileOut =
                new FileOutputStream("c:\\TSTPersistentStore.ser",true);
            AppendingObjectOutputStream appendingObjectOutputStream=new AppendingObjectOutputStream(fileOut);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            appendingObjectOutputStream.writeStreamHeader();
            appendingObjectOutputStream.writeObject(e);
            appendingObjectOutputStream.close();
            fileOut.close();
            System.out.printf("Serialized data is appended in /tmp/employee.ser");
        } catch(IOException i) {
            i.printStackTrace();
        }
    }
}


package com.company;
import java.io.*;

public class AppendingObjectOutputStream extends ObjectOutputStream {

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

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

}


package com.company;

public class Employee implements java.io.Serializable {
    public String name;
    public String address;
    public transient int SSN;
    public int number;

    public void mailCheck() {
        System.out.println("Mailing a check to " + name + " " + address);
    }
}

package com.company;


import java.io.*;
public class DeserializeDemo {
    public static void main(String [] args) {
        Employee e = null;
        try {
            FileInputStream fileIn = new FileInputStream("c:\\TSTPersistentStore.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            e = (Employee) in.readObject();
            e = (Employee) in.readObject();
            in.close();
            fileIn.close();
        } catch(IOException i) {
            i.printStackTrace();
            return;
        } catch(ClassNotFoundException c) {
            System.out.println("Employee class not found");
            c.printStackTrace();
            return;
        }
        System.out.println("Deserialized Employee...");
        System.out.println("Name: " + e.name);
        System.out.println("Address: " + e.address);
        System.out.println("SSN: " + e.SSN);
        System.out.println("Number: " + e.number);
    }
}

I can populate the objects into the file as I see the file size increasing. However I cannot deserialize them back into an object and run into an 'java.io.StreamCorruptedException: invalid type code: AC'.

I have gone though suggestions by @Andreas_D and @EJP but am unable to incorporate it into the code.

Can somebody please help me understand the use of the reset() method that we use to avoid rewriting the header into the .ser file. ? I am unable to prevent the header from being written into the file.

Please let me know if you need any further information.

Martin
  • 122
  • 9
Chicky_D
  • 1
  • 2
  • 2
    I'd store objects in json in your place. Like in [this tutorial](http://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/) – Iwo Kucharski Apr 13 '16 at 11:17
  • I'm not sure you should call `reset()` since it states: `The state is reset to be the same as a new ObjectOutputStream.` Try overwriting `writeStreamHeader()` with just an empty body. Also note that `ObjectOutputStream(...)` will internally call `writeStreamHeader()` thus you should only create that line if the file is empty, otherwise each time you call that constructor you're writing the header to the file again. – Thomas Apr 13 '16 at 11:25

0 Answers0