1

I have a Map:

Map<String, DistributorAdd> map= new TreeMap<String, DistributorAdd>();

and I save it in a file.txt

FileOutputStream  fos = new FileOutputStream("Distrib.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(map);
        oos.close();

The problem is that Distributor who was yesterday like:

public DistributorAdd(String distributor, String 
        emailAdress, String name, String speciality){...}

Will be tomorrow like this:

public void ajouter(String Distributor, String EmailAdress, 
    String Name, String Phone, String Image) {..}

My coworker already placed a lot of info in her Distrib.txt so what I want is to be able to put a new String in the Map without destroying it.

I would like to keep Distrib.txt and my DistributorAdd function is there any easy step I could do to do that?

The kind of error I get is:

ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Distrib.txt"));
VendorA = (DistributorAdd) ois.readObject();

Error:

IOException : table.java => table()java.io.StreamCorruptedException: invalid stream header: ACED0573
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:277)
at car.Table.<init>(Table.java:185)
at car.Table.main(Table.java:837)

If you have any question or any more information that I need to give I will be happy to do it.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Nico
  • 93
  • 5

1 Answers1

0

Please note following things about serialization:

1)It has to be used very judiciously.

2)When we serialise a class , it is like we are exporting its API in terms of instance variables.

3)Always explicitely declare SerialVersionUID in your serializable class,if we don't it will be automatically calculated by JVM based on internal structure of your class (instance vars,public methods etc.) so changing internal strcture of class chnages this SerialVersionUID.

Hence , If we serailise a class in one version and then deserialise it in another version (by version change I mean we are changing some internal structure of serializable object) , version incompatibility is bound to happen.

I am not sure what exactly may have caused for your code to fail since I don't know what chnages are you doing in your Serializable class.

But I think you should consider point 3 once

Lakshmi
  • 2,204
  • 3
  • 29
  • 49
Atul
  • 1,560
  • 5
  • 30
  • 75
  • Thanks for your help I understand now better why it doesn't work and will try to understand better Serialization before using it again. A intresting link for it: [link](http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it) – Nico Apr 12 '13 at 08:56