Serialization of Java Objects

Source: Internet
Author: User

We know that when Java creates an object, it will persist as long as you need it, but it will not continue to exist at any time when the program terminates. While this certainly makes sense, there are still situations where it is useful to have objects that can still exist and save their information if the program does not run. That way, the next time you run the program, the object will be rebuilt and has the same information as the program last ran.

Java serialization transforms the object that implements the serializable interface into a sequence of bytes, and is able to fully revert the byte serialization to its original object at a later time. This process can even be done over the network, which means that the serialization mechanism can automatically compensate for differences between different operating systems. This means that an object created on a Windows operating system, if you're transmitting it over a network to a UNIX machine, is reassembled there without worrying that the data will be different on different machines.

In itself, the serialization of objects is interesting because it allows for lightweight persistence. "Persistence" means that the life cycle of an object does not depend on whether the program is executing, it can survive calls between programs. (This is why Android can pass the serialized object between different activity, and of course, Android has its own serialized interface, Parcelable).

To serialize an object, you first create some OutputStream objects, and then encapsulate them within a Objectoutput-stream object. At this point, you only need to call WriteObject () to serialize the object.

Deserialization requires that a inputstreame be encapsulated within Objectinputstreame and then called ReadObject.


public class Worm implements serializable{/** * */private static final long serialversionuid = 1l;private static Random r Andom = new Random ();p rivate data[] D = {new data (Random.nextint), new data (Random.nextint), new data ( Random.nextint)};p rivate worm next;private char c;public worm (int I,char x) {System.out.println ("Worm consturctor:" + i); c = x;if (--> 0) {next = new Worm (i, (char) (x+1));}} Public Worm () {System.out.println ("Default consructor");} @Overridepublic String toString () {return "Worm [d=" + arrays.tostring (d) + ", next=" + Next + ", c=" + c+ "]";} public static void Main (string[] args) throws exception{worm worm = new worm (6, ' a '); System.out.println ("W1" +worm); ObjectOutputStream objectoutputstream = new ObjectOutputStream (New FileOutputStream (" Worm.out ") objectoutputstream.writeobject (" Worm storage\n "); Objectoutputstream.writeobject (worm); O Bjectoutputstream.close (); ObjectInputStream inputstream = new ObjectInputStream (New FileInputStream ("Worm.out")); StrinG S = (String) inputstream.readobject (); System.out.println (s); Worm Worm2 = (worm) inputstream.readobject (); System.out.println ("W2" +worm2); Inputstream.close (); Bytearrayoutputstream Arrayoutputstream = new Bytearrayoutputstream (); ObjectOutputStream objectOutputStream2 = new ObjectOutputStream (Arrayoutputstream); Objectoutputstream2.writeobject (worm); Objectoutputstream2.flush (); O Bjectinputstream inputStream2 = new ObjectInputStream (New Bytearrayinputstream (Arrayoutputstream.tobytearray ())); Worm worm3 = (worm) inputstream2.readobject (); System.out.println ("W3" +WORM3);} class data implements Serializable{private int n;//private transient int n; Let the field avoid serializing public Data (int n) {super (); THIS.N = n; @Overridepublic String toString () {return "Data [n=" + n + "]";}}


Serialization of Java Objects

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.