publicObject deepClone(Object obj){
//将对象写到流里
ByteArrayOutoutStream bo=newByteArrayOutputStream();
ObjectOutputStream oo=newObjectOutputStream(bo);
oo.writeObject(obj);
//从流里读出来
ByteArrayInputStream bi=newByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi=newObjectInputStream(bi);
return(oi.readObject());
}
//===================================================================================
泛型版本:
@SuppressWarnings("unchecked")
private static <T> T copy(T t) throws Exception {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(t);
ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi = new ObjectInputStream(bi);
return (T) (oi.readObject());
}
//将对象写到流里
ByteArrayOutoutStream bo=newByteArrayOutputStream();
ObjectOutputStream oo=newObjectOutputStream(bo);
oo.writeObject(obj);
//从流里读出来
ByteArrayInputStream bi=newByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi=newObjectInputStream(bi);
return(oi.readObject());
}
//===================================================================================
泛型版本:
@SuppressWarnings("unchecked")
private static <T> T copy(T t) throws Exception {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(t);
ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi = new ObjectInputStream(bi);
return (T) (oi.readObject());
}