Savage F. Morgan

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
public static Object copy(Object oldObj) {  

    Object obj = null;  

    try {  

        // Write the object out to a byte array  

        ByteArrayOutputStream bos = new ByteArrayOutputStream();  

        ObjectOutputStream out = new ObjectOutputStream(bos);  

        out.writeObject(oldObj);  

        out.flush();  

        out.close();  

        // Retrieve an input stream from the byte array and read  

        // a copy of the object back in.  

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());   

        ObjectInputStream in = new ObjectInputStream(bis);  

        obj = in.readObject();  

    } catch (IOException e) {  

        e.printStackTrace();  

    } catch (ClassNotFoundException cnfe) {  

        cnfe.printStackTrace();  

    }  

    return obj;  

}  

 

posted on 2013-01-25 08:17  罗斯摩根  阅读(187)  评论(0编辑  收藏  举报