From:
Http://www.cnblogs.com/DreamDrive/p/5430981.html
Requires object implementation Serializable interface
public class Cloneutils {
Copy an Object
@SuppressWarnings ("Unchecked")
public static <t extends serializable> t clone (t obj) {
Copy the resulting object
T clonedobj = null;
try {
Reading object byte data
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
ObjectOutputStream oos = new ObjectOutputStream (BAOs);
Oos.writeobject (obj);
Oos.close ();
Allocate memory space, write to original object, generate new Object
Bytearrayinputstream Bais = new Bytearrayinputstream (Baos.tobytearray ());
ObjectInputStream ois = new ObjectInputStream (Bais);
Returns a new object and makes a type conversion
Clonedobj = (T) ois.readobject ();
Ois.close ();
} catch (Exception e) {
E.printstacktrace ();
}
return clonedobj;
}
}
Java Object Deep Copy tool