Created
October 13, 2012 21:23
-
-
Save kirang89/3886188 to your computer and use it in GitHub Desktop.
Serializing and deserializing an object in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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