???????л??????л?
???????л? (Serialization)?????????????????????洢????????????????????????洢?????????y?飬???絵??????????建?????????紫??????У?????????????XML?????????????XML?????????????????????????????????????????л???
????Java????????л??????л?
??????Java?У???????????????????????????????????????б?????????????????????????????????????????ЩJava???????????JVM???????е?????JVM?????????????????Щ???????????????JVM?????У???Щ???????????????????
?????????????????ó????У????????????Щ??????????????????????????????????????????????Java????????л???????????????ù????
???????????л??????object serialization????Java?????????????????????????????????л?????????????????????????飬?????????????????????????????????????л??????????????????????л?????????????JVM?е?????????????饗???????????????
??????Java?У?????????л??????л?????????RMI(??????????)?????紫???С?
??????????????
????Java?????????????Java??????????л????????л????????????API?????????а????????????
????java.io.Serializable
????java.io.Externalizable
????ObjectOutput
????ObjectInput
????ObjectOutputStream
????ObjectInputStream
????Serializable ???
???????????? java.io.Serializable ??????????????л??????δ????????????????κ??????л??????л????????л????????????????????????л???????л??????з???????Σ??????????????л??????塣 (?y?????з???????Σ????????????y????????????????л????)
????????????????????????л?????????????????? Serializable ??????????????£??????NotSerializableException??
???????????л??????и????????????????ж?????????????????????????????ü???java.io.Serializable????
??????????????????java.io.Serializable??????
package com.hollischaung.serialization.SerializableDemos;
import java.io.Serializable;
/**
* Created by hollis on 16/2/17.
* ???Serializable???
*/
public class User1 implements Serializable {
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 age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
"?? age=" + age +
'}';
}
}
?????????????????????л????????л?
package com.hollischaung.serialization.SerializableDemos;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import java.io.*;
/**
* Created by hollis on 16/2/17.
* SerializableDemo1 ???SerializableDemo2??? ???????????л????????Serializable???
*/
public class SerializableDemo1 {
public static void main(String[] args) {
//Initializes The Object
User1 user = new User1();
user.setName("hollis");
user.setAge(23);
System.out.println(user);
//Write Obj to File
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("tempFile"));
oos.writeObject(user);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(oos);
}
//Read Obj from File
File file = new File("tempFile");
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(file));
User1 newUser = (User1) ois.readObject();
System.out.println(newUser);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(ois);
try {
FileUtils.forceDelete(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//OutPut:
//User{name='hollis'?? age=23}
//User{name='hollis'?? age=23}
???????????Serializable????????ο????????