Serialization of Java Objects

Source: Internet
Author: User

Object serialization: Convert an object directly into a byte stream and then write it to a binary file on the hard disk or over the network, and again, we can read the binary into memory and use it to initialize an object that requires a serialized class object. It is necessary to implement the Serializable interface (null interface, which is used in conjunction with instance of the flag), do not need to serialize the property can be modified with transient.

Class:
ObjectOutputStream
ObjectInputStream

Common methods:
ReadObject
WriteObject

It's easy to think of the problem of serializing an object with one pointer pointing to another object, and we should also serialize the object that the pointer is pointing to. In addition, when the object is reorganized after reading, the referenced content should also be modified synchronously.

In C + + notes to do a simple implementation of the program, you can refer to



A basic sample code

class student implements serializable{  public string name;   Transient public float score;  public student (String name,float score)  {  this.name = name;  this.score = score; }}public  Class testio {   public static void main (String[] args)  {     student ss = new student ("Liha",  95.5f);     file file = new file ("D:\\object.dat");    try {    objectoutputstream objout = new objectoutputstream (New FileOutputStream ( file));    objout.writeobject (ss);      objectinputstream  Objin = new objectinputstream (New fileinputstream (file));   student  sbackstudent =   (Student) Objin.readobject ();       system.out.println (SbackStudent.name);    system.out.println (Sbackstudent.score);       objout.close ();    objin.close ();     } catch  (filenotfoundexception e)   {   // todo auto-generated catch block   e.printstacktrace () ;  } catch  (ioexception e)  {   // todo auto-generated  catch block   e.printstacktrace ();  } catch  ( Classnotfoundexception e)  {   // todo auto-generated catch block    e.printstacktrace ();   }   }}



This article is from the "Technical Life" blog, please be sure to keep this source http://6621180.blog.51cto.com/6611180/1922798

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.