Java Object Deep Clone deepClone(Object objToClone)

Here you can find the source of deepClone(Object objToClone)

Description

deep Clone

License

Apache License

Declaration

public static Object deepClone(Object objToClone) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    public static Object deepClone(Object objToClone) {
        try {//from w ww. ja  v a2s.  com
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(objToClone);
            ByteArrayInputStream bais = new ByteArrayInputStream(
                    baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
            Object deepCopy = ois.readObject();

            return deepCopy;
        } catch (Exception e) {
            System.out.println("deep clone failed");
            e.printStackTrace();
            return null;
        }

    }
}

Related

  1. deepClone(final E object)
  2. deepClone(final T objectToBeClonned)
  3. deepClone(Object obj)
  4. deepClone(Object src)
  5. deepClone(Object src)
  6. deepClone(Serializable o)
  7. deepClone(Serializable serializable)