对象的深拷贝(结合泛型)

/**
 * 通过序列化进行深克隆   DeepCopyUtil
 *
 * @author leihz
 * @date 2017/11/28 13:05
 */
public class DeepCopyUtil {

    public static <T> T deepClone(T obj) throws IOException, ClassNotFoundException {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        // 将流序列化成对象
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (T) ois.readObject();
    }


}
原文地址:https://www.cnblogs.com/leihuazhe/p/7909636.html