Serialization of Java objects cloning __java

Source: Internet
Author: User

Brief Introduction:

Copying Java objects in a byte-stream way


Code:

Stream Clone copy function

    public static object Deepclone (Object obj) {
        if (obj = = null) {return
            null;
        }
        try {
            Bytearrayoutputstream byteout = new Bytearrayoutputstream ();
            Object cloneobj = null;
            ObjectOutputStream out = new ObjectOutputStream (byteout);
            Out.writeobject (obj);
            Bytearrayinputstream Bytein = new Bytearrayinputstream (Byteout.tobytearray ());
            ObjectInputStream in = new ObjectInputStream (Bytein);
            Cloneobj = In.readobject ();
            return cloneobj;
        } catch (Exception e) {
            throw new RuntimeException (e);
        }
    }


Objectclone.java

Package test;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;

Import java.io.Serializable; public class Objectclone {/** * Test object/private static class person implements Serializable {p
        Rivate String name;
        private int age;
            Person (String name, int age) {this.name = name;
        This.age = age;
        @Override public String toString () {return String.Format (' name:%s\nage:%s ', name, age); }/** * Test function */public static void main (string[] args) {Person person = new P
        Erson ("Henry", 22);
        Person Newperson = (person) objectclone.deepclone (person);
    System.out.println (Newperson); 
        /** * Tool class * * @param obj * @return/public static object Deepclone (Object obj) { if (obj = null) {returnNull
            try {bytearrayoutputstream byteout = new Bytearrayoutputstream ();
            Object cloneobj = null;
            ObjectOutputStream out = new ObjectOutputStream (byteout);
            Out.writeobject (obj);
            Bytearrayinputstream Bytein = new Bytearrayinputstream (Byteout.tobytearray ());
            ObjectInputStream in = new ObjectInputStream (Bytein);
            Cloneobj = In.readobject ();
        return cloneobj;
        catch (Exception e) {throw new RuntimeException (e);
 }
    }
}


Test results:




、







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.