java -深拷贝

@SuppressWarnings("unchecked")
public static <T extends Serializable> T deepCopy(T object) throws IOException, ClassNotFoundException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;
    try {
        oos = new ObjectOutputStream(bos);
        oos.writeObject(object);
        oos.flush();
        ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
        ois = new ObjectInputStream(bin);
        return (T) ois.readObject();
    } finally {
        if (oos != null) {
            oos.close();
        }
        if (ois != null) {
            ois.close();
        }
    }
}

 

posted @ 2024-07-03 21:33  软工风少  阅读(10)  评论(0)    收藏  举报