Serializing
Storing an objet-graph to a byte stream
Deserializing
Restoring an objet-graph from a byte stream
Serializing an Object
- ObjectOutputStream
ObjectOutputStream out = new ObjectOutputStream( Files.newOutputStream (Path.get( filename )) );
out.writeObj ( object );
Deserializing an Object
- ObjectInputStream
ObjectInputStream ois = new ObjectInputStream( Files.newInputStream (Path.get( filename )) );
ois.readObj ( );
Creating class version compatibility
- In a propmt inside /bin from our JDK
- Type:> serialer CLASS_PATH(Ej. com.comp.class)
or
- Type: serialer -show
To have a GUI
- Delcare the field serialVersionUID as
As long as all our classes contains the same serialVersionUID number will be compatible among them.
Compare version using Reflection
Once you get the ObjectOutputStream you can start using reflection.
ObjectOutputStream object = .....;
object.get("id", null);
In this case we are validating if this version contains the field Id
Transient Fields
Use transient to avoid that some fields be serialized.
Ej. private transient int total;
Comentarios
Publicar un comentario