<pre name="code" class="java">public class MyUtil {
private MyUtil() {
throw new AssertionError();
}
@SuppressWarnings("unchecked")
public static <T extends Serializable> T deepClone(T o) throws Exception {
// 将对象序列化到内存中
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(o);
// 从内存中反序列化对象
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream ois = new ObjectInputStream(in);
return (T) ois.readObject();
}
}
序列化与反序列化
最新推荐文章于 2024-04-11 15:24:39 发布