Java object serialization to serialize the contents of

Source: Internet
Author: User
Tags object serialization serialization

1.java object serialization, saving only the object's type information and property type and property values, three parts of the data. The method in the object, no matter how many, is not serialized.

Package Com.hs.javafan; Import java.io.Serializable; public class Serializableobject implements Serializable {private string name, private int age, private string address, PU Blic String GetName () {return name.} public void SetName (String name) {this.name = name.} public int getage () {return Age public void Setage (int age) {this.age = age;} public string toString () {return ' Test ';} public string getaddress () { return address; public void setaddress (String address) {this.address = address;}}

Remove:

Public String toString () {
return "Test";
}

Package Com.hs.javafan; Import java.io.Serializable; public class Serializableobject implements Serializable {private string name, private int age, private string address, PU Blic String GetName () {return name.} public void SetName (String name) {this.name = name.} public int getage () {return Age public void Setage (int age) {this.age = age;} public String getaddress () {return address;} public void Setaddress (St Ring address) {this.address = address;}}

Package Com.hs.javafan; Import java.io.FileNotFoundException; Import Java.io.FileOutputStream; Import java.io.IOException; Import Java.io.ObjectOutputStream; public class Serializabletest {/** * @param args */public static void main (string[] args) {Serializableobject Serializa Bleobject = new Serializableobject (); Serializableobject.setname ("Kaiserfan"); Serializableobject.setage (30); Serializableobject.setaddress ("Hangzhou"); try {fileoutputstream out = new FileOutputStream ("test.txt"); ObjectOutputStream objectout = new ObjectOutputStream (out); Objectout.writeobject (Serializableobject); Out.close (); Objectout.close (); catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();}} }

The file size result is the same.

Regardless of how many methods are added to the Serializableobject object, serialization saves only the object type, the property type, and the property value, which three data information.

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.