Skip to content

Instantly share code, notes, and snippets.

@kirang89
Created October 13, 2012 21:23
Show Gist options
  • Save kirang89/3886188 to your computer and use it in GitHub Desktop.
Save kirang89/3886188 to your computer and use it in GitHub Desktop.
Serializing and deserializing an object in Java
public class Serializer {
public static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(b);
o.writeObject(obj);
return b.toByteArray();
}
public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream b = new ByteArrayInputStream(bytes);
ObjectInputStream o = new ObjectInputStream(b);
return o.readObject();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment