美文网首页
Java深度克隆

Java深度克隆

作者: 业余的猫 | 来源:发表于2016-12-30 16:11 被阅读0次
@SuppressWarnings("unchecked")
public static <T extends Serializable> T deepClone(T object) {
    T temp = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
        ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        temp = (T) ois.readObject();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return temp;
}

相关文章

网友评论

      本文标题:Java深度克隆

      本文链接:https://www.haomeiwen.com/subject/pgvmvttx.html