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

时间:2022-04-30 19:55:58
/** * 通过序列化进行深克隆 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(); } }