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;  

}  

 

相关文章:

  • 2022-12-23
  • 2021-12-21
  • 2022-12-23
  • 2021-10-25
  • 2022-12-23
  • 2021-12-28
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
  • 2021-10-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案