Serialize and Deserialize Objects


/ Published in: Java
Save to your folder(s)

The given utility class can be used to serialize and deserialize java objects


Copy this code and paste it in your HTML
  1. package serialize;
  2.  
  3. import java.io.IOError;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.io.OutputStream;
  9. import java.io.Serializable;
  10.  
  11. public class IOUtils {
  12. public static void serialize(OutputStream out, Serializable obj) {
  13. try {
  14. stream.writeObject(obj);
  15. } catch (IOException e) {
  16. throw new IOError(e);
  17. }
  18. }
  19.  
  20. public static <T> T deserialize(InputStream in, Class<? extends T> c) {
  21. try {
  22. T obj = c.cast(stream.readObject());
  23. return obj;
  24. } catch (IOException e) {
  25. throw new IOError(e);
  26. } catch (ClassNotFoundException e) {
  27. throw new IOError(e);
  28. }
  29. }
  30. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.