Java Object Deep Copy tool

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.