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:
Serialization is a better way to use class persistence
Can be used for remote method calls such as soap
A method of distributing objects, especially in software components, such as COM CORBA
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