Serialization util

Serialization is a method to save objects and all of its sub-objects recursively to harddrive for later use. The objects “exist” in a file even outside runtime.

Sometimes it is neccessary to save information (objects) to the harddrive and using them later in the exact same state (called “persistent objects”) again (for ex. when application restarts). With this simple helperclass you can easily save every object and re-load it from a file.
The objects need to implement the Serializeable-Interface (adds no methods, just marker-interface).
It saves any instance-fields; to make it ignore fields for saving add a transient-modifier to the field.
Download the file here : Download

Create a new instance where <T> is your type

Serilizer<T> s = new Serilizer<>();

Save an object

T obj = new T();
try {
    s.save(obj,"path/to/file.ending");
} catch (SerializeException e) { /* handle exception */ }

Load an object

try {
    T obj = s.load("path/to/file.ending");
} catch (SerializeException e) { /* handle exception */ }

 

Notes:

If a¬†NotSerializableException¬†occurs, it could be that a member-object of the object you tried to serialize isn’t serializable.