Serialization and deserialization of objects

Source: Internet
Author: User
Tags object serialization

1) to make an object a serialized object, this class implements the Serializable or Externalizable interface (Externalizable interface inheritance and serializable interface).

This serialization only serializes the object's non-transient instance variables , does not serialize the static member variables, and does not serialize the method

2)

Object serialization: The process of translating a Java "object" into a "byte sequence" is called object serialization

Object deserialization: The process of reverting a "byte sequence" to a Java "object" is called deserialization

Does not invoke any of the constructor methods of the class when deserializing

3)

Function description for object serialization:

1. Convert objects into bytes to the hard disk, usually stored in a file, when you open the file is a bit garbled, but also can see the corresponding information

2. Passing the byte sequence of an object on the network

Specific examples:

[Java]View plain copy
  1. Import java.io.Serializable;
  2. Import Java.io.ObjectOutputStream;
  3. Import Java.io.FileOutputStream;
  4. Import java.io.*;
  5. Serialization and deserialization of objects
  6. Public class Serializabletest
  7. {
  8. public static void Main (string[] args)
  9. {
  10. Person p = new Person ("xiaoming",172.0);
  11. person P2 = new Person ("Little Red",165.0);
  12. Person P3 = new Person ("Floret",168.0);
  13. Try
  14. {
  15. FileOutputStream fos = new FileOutputStream ("E:/abc/person.txt");
  16. //Implement object serialization to write objects into a file in a byte-stream format
  17. ObjectOutputStream Oos = new ObjectOutputStream (FOS);
  18. //object serialization can only write "non-static member variable" to the stream, static member variable, or the method can not be written into the stream
  19. Oos.writeobject (P);
  20. Oos.writeobject (p2);
  21. Oos.writeobject (p3);
  22. Oos.close ();
  23. //Deserialize, write the object in the file back into the program (memory)
  24. FileInputStream FIS = new FileInputStream ("E:/abc/person.txt");
  25. ObjectInputStream ois = new ObjectInputStream (FIS);
  26. Person pe1 = (person) ois.readobject ();
  27. Person pe2 = (person) ois.readobject ();
  28. Person pe3 = (person) ois.readobject ();
  29. System.out.println (pe1.name+"," + Pe1.age + "," + pe1.height ");
  30. System.out.println (pe2.name+"," + Pe2.age + "," + pe2.height ");
  31. System.out.println (pe3.name+"," + Pe3.age + "," + pe3.height ");
  32. Ois.close ();
  33. }
  34. catch (Exception e)
  35. {
  36. E.printstacktrace ();
  37. }
  38. }
  39. }
  40. Class Person implements Serializable //The object implements the Serializable interface, which is a serialized object
  41. {
  42. String name;
  43. int age;
  44. double height;
  45. Public person (String name, int. Age, double height)
  46. {
  47. this.name = name;
  48. this.age = age;
  49. this.height = height;
  50. }
  51. }

If the user wants to control how the class is serialized, you can provide the following form of the WriteObject () and ReadObject () methods in the Serializable class, which you can refer to the API.

private void WriteObject (Java.io.ObjectOutputStream out) throws IOException
private void ReadObject (Java.io.ObjectInputStream in) throws IOException, ClassNotFoundException

Serialization and deserialization of 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.