Super-simple object serialization in Java

If your software produces costly objects, object serialization may be an option to spare you some bootstrapping time, e.g., when you repeatedly restart your application during development. Apache Commons-Lang offers an implementation of serialization that is an epitome of ease of use: SerializationUtils. The core methods are serialize and deserialize.

Given an object, the actual process of serialization is a one-line statement (split up here):

final File targetFile = new File("./target/serializedObject.ser");
final BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(targetFile));
SerializationUtils.serialize(object, outStream);

The same holds for the deserialization process. In these subsequent lines, we assume that the object to be derserialized is an instance of java.lang.String:

final BufferedInputStream inStream = new BufferedInputStream(new FileInputStream(targetFile));
final String string = (String) SerializationUtils.deserialize(inStream);

A complete executable example can be found on GitHub.

Maven Dependency (a Jar file for download can be found here):



  • [1] SerializationUtils Javadoc
  • [2] Executable example code (Maven project)

Leave a Reply