A simple example of Java implementation serialization and deserialization _java

Source: Internet
Author: User
Tags serialization stringbuffer

1.Java Serialization and deserialization

Java serialization refers to the process of converting a Java object into a sequence of bytes, while Java deserialization refers to the process of restoring a byte sequence to a Java object.

2. Why serialization and deserialization are needed

We know that when two processes are communicating remotely, each type of data can be sent to each other, including text, pictures, audio, video, and so on, and the data is transmitted over the network in the form of a binary sequence. So when two Java processes are communicating, can you implement object transfer between processes? The answer is yes. How do you do that? This requires Java serialization and deserialization. In other words, on the one hand, the sender needs to convert this Java object into a byte sequence, and then on the network
, on the other hand, the receiver needs to recover the Java object from the byte sequence.

When we understand why Java serialization and deserialization are needed, it is natural to think of the benefits of Java serialization. One of the benefits is the persistence of data, which can be permanently saved to the hard disk (usually in a file) by serialization, and the second is to implement remote communication using serialization, that is, to transfer the byte sequence of objects over the network.

3. Example:
(1) Serialization of deserialization files:

Import java.io.*; @SuppressWarnings ("serial") class person implements Serializable {public person (string name, string sex, int age, I 
    NT height) {this.name = name; 
    This.sex = sex; 
    This.age = age; 
  This.height = height; 
        Public String toString () {return "|" + THIS.name + "|" + This.sex + "|" + This.age + "|" 
  + this.height + "|"; 
  public String name; 
  public String sex; 
  public int age; 
public int height;  public class Serialtest {public static void main (string[] args) throws FileNotFoundException, IOException, 
 
    classnotfoundexception {Person p = new person ("Jim", "male", 28, 194); 
    Begins serialization of ObjectOutputStream Oos = new ObjectOutputStream (New FileOutputStream ("MyTest.txt")); 
 
    Oos.writeobject (P); 
    Deserialization ObjectInputStream ois = new ObjectInputStream (New FileInputStream ("MyTest.txt")); 
 
person P1 = (person) ois.readobject ();    System.out.println (P1.tostring ()); 
 } 
}


(2) XML deserialization to class:

Import java.io.*; 
Import Com.thoughtworks.xstream.XStream; 
 
 
Import Com.thoughtworks.xstream.io.xml.DomDriver; 
  @SuppressWarnings ("Serial") class Roadinfo implements Serializable {public int id; 
  public long MDN; 
  Public String NAME; 
  public double LNG; 
  public double LAT; 
 
Public String ICON; @SuppressWarnings ("Serial") class Table_list implements Serializable {public String toString () {Stringbu 
    Ffer sb = new StringBuffer (); 
      for (Roadinfo r:sequence) {sb.append ("|"); 
      Sb.append (r.id); 
      Sb.append ("|"); 
      Sb.append (R.MDN); 
      Sb.append ("|"); 
      Sb.append (R.name); 
      Sb.append ("|"); 
      Sb.append (R.LNG); 
      Sb.append ("|"); 
      Sb.append (R.lat); 
      Sb.append ("|"); 
      Sb.append (R.icon); 
    Sb.append ("|\n"); 
  return sb.tostring (); 
    Public table_list (int count) {sequence = new Roadinfo[count]; for (int i = 0; i < count; i++) {Sequence[i] = newRoadinfo (); 
} public roadinfo[] sequence;  
    public class Xmltest {/** * @param args */public static void main (string[] args) throws Exception { 
    TODO auto-generated Method stub stringbuffer sb = new StringBuffer (); 
    BufferedReader reader = new BufferedReader (new FileReader) (New File ("Friend_msg.xml")); 
      while (true) {String s = reader.readline ();//Read line if (s = = null) {break; 
    } sb.append (s); 
    } xstream xs = new XStream (new Domdriver ()); 
    Table_list db = (table_list) xs.fromxml (sb.tostring ()); 
 
  System.out.println (Db.tostring ()); 
 } 
}

Related Article

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.