Java Foundation 15th Day _IO serialization/deep copy

Source: Internet
Author: User

1. Use Randomaccessfile to achieve file compositing.

2. Use serialization technology in Java to implement an object graph (100 cat cat collections, but cat owner does not need serialization) for deep replication.

3. Describe the serialization of the process, application scenarios, the purpose of serial ID.

===========================================================================

1. Use Randomaccessfile to achieve file compositing.

2. Use serialization technology in Java to implement an object graph (100 cat cat collections, but cat owner does not need serialization) for deep replication.

Package com.it18zhang15;


Import Java.io.FileInputStream;

Import java.io.FileNotFoundException;

Import Java.io.FileOutputStream;

Import Java.io.ObjectInputStream;

Import Java.io.ObjectOutputStream;

Import java.util.ArrayList;

Import java.util.List;


public class Catcopedemo {


/**

* @param args

* @throws Exception

* @throws FileNotFoundException

*/

@SuppressWarnings ("Unchecked")

public static void Main (string[] args) throws Exception {

List<cat> list=new arraylist<cat> ();

for (int i=0; i<100; i++) {

List.add (New Cat ("Walnut" +i,i));

}

///*

Write serial

ObjectOutputStream oos=new ObjectOutputStream (New FileOutputStream ("D:/test/cat.txt"));

Oos.writeobject (list);

Oos.close ();

//*/

List<cat> list1=new arraylist<cat> ();

///*

Read-out serial

ObjectInputStream ois=new ObjectInputStream (New FileInputStream ("D:/test/cat.txt"));

list1= (list<cat>) ois.readobject ();

Ois.close ();

//*/

Output collection

for (int i=0; i<100; i++) {

System.out.println (List1.get (i). GetName () + "yes" +list1.get (i). Getage () + "year-old");

}

}

}

Package com.it18zhang15;


Import java.io.Serializable;


public class Cat implements Serializable {


/**

*

*/

The following sentence is a version of Serializable, which, if added later, will not be an error if the class is called.

Private static final long serialversionuid = -5446106533693934226l;

private String name;

private int age;

private transient person owner;

Public Cat () {

Super ();

}

Public Cat (String name, int., person owner) {

Super ();

THIS.name = name;

This.age = age;

This.owner = owner;

}

Public Cat (String name, int age) {

Super ();

THIS.name = name;

This.age = age;

}

Public Person GetOwner () {

return owner;

}

public void SetOwner (person owner) {

This.owner = owner;

}

Public String GetName () {

return name;

}

public void SetName (String name) {

THIS.name = name;

}

public int getage () {

return age;

}

public void Setage (int.) {

This.age = age;

}


}

Package com.it18zhang15;


public class Person {

private String name;

private int age;

Public String GetName () {

return name;

}

public void SetName (String name) {

THIS.name = name;

}

public int getage () {

return age;

}

public void Setage (int.) {

This.age = age;

}

}

3. Describe the serialization of the process, application scenarios, the purpose of serial ID.

serialization ( Serialization binary mode is transmitted over the network. You can then re-construct the same

1, the class of the object must implement the Serializable interface.

2, the serialized member of the object must be a non-static member variable, which means that no member method and static member variable can be saved, and serialization saves only the value of the variable and cannot be saved for any modifier of the variable. fileinputstream object, for these fields, we must use the transient keywords are indicated. (Note: Saving any member method and static member variable does not make any sense, because the class of the object has completely saved them)

3, to serialize an object, must be associated with a certain object input/output stream, through the object output stream to save the object state, and then through the object input stream to restore the object state.

Serialization has but not limited advantages:

    1. Serialization is a better way to use class persistence

    2. Can be used for remote method calls such as soap

    3. A method of distributing objects, especially in software components, such as COM CORBA

    4. Changes in data detection over time

This article is from the "Jobs" blog, so be sure to keep this source http://10718270.blog.51cto.com/10708270/1785520

Java Foundation 15th Day _IO serialization/deep copy

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.