I'm having some problems regarding serialization and polymorphic arguments.
My project has an array of Aquariums and I want to serialize each one to later deserialize them.
Below is the Sim constructor from Sim class which deals with Aquarium serialization.
I don't really know why the o.writeObject(aqua) doesn't work as intended(which is to serialize an Aquarium which is given as a polymorphic argument).Neither does aq=(Aquarium) in.readObject().
I suspect it has something to do with the polymorphic arguments being passed on the methods.
Sim{
...
for(int i=0;i<aquariumssize();i++)
serializeAquarium(aquariums.get(i));
}
public void serializeAquarium(Aquarium aqua)
{
try {
System.out.println(aqua.getNameAquarium());
ObjectOutputStream o=new ObjectOutputStream(new FileOutputStream(aqua.getNameAquarium()+".dat"));
o.writeObject(aqua);
o.close();
}
catch (Exception e)
{
System.out.println("Couldn't not serialize!");
}
}
public Aquarium deserializeAquarium(String name)
{
Aquarium aq=null;
try {
FileInputStream fileIn =new FileInputStream(name+".dat");
ObjectInputStream in = new ObjectInputStream(fileIn);
aq=(Aquarium) in.readObject();
in.close();
}
catch (Exception e)
{
System.out.println("Couldn't deserialize!");
}
return aq;
}
Thanks in advance for the help!


Sign In
Create Account

Back to top









