io

Serialize arbitrary objects

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: November 11th, 2012
0 89 1 minute read
public static byte[] serializeObject(Serializable object) throws Exception {  ByteArrayOutputStream baos = null;  ObjectOutputStream oos = null;  byte[] res = null;    try {   baos = new ByteArrayOutputStream();   oos = new ObjectOutputStream(baos);      oos.writeObject(object);   oos.flush();      res = baos.toByteArray();    } catch (Exception ex) {   throw ex;  } finally {   try {    if(oos != null)     oos.close();   } catch (Exception e) {    e.printStackTrace();   }  }    return res; }
public static Serializable deserializeObject(byte[] rowObject) throws Exception {  ObjectInputStream ois = null;  Serializable res = null;    try {      ois = new ObjectInputStream(new ByteArrayInputStream(rowObject));   res = (Serializable) ois.readObject();    } catch (Exception ex) {   throw ex;  } finally {   try {    if(ois != null)     ois.close();   } catch (Exception e) {    e.printStackTrace();   }     }    return res;   }

Related Article:

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Ilias TsagklisIlias TsagklisNovember 11th, 2012Last Updated: November 11th, 2012
0 89 1 minute read
Photo of Ilias Tsagklis

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor atJava Code Geeks.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.