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

/**
 * 通过序列化进行深克隆   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();
    }


}
posted @ 2017-11-28 14:05  枫叶maple  阅读(904)  评论(0编辑  收藏  举报