????Externalizable???
????????Serializable ???java?л?????????????л????Externalizable
??????????Externalizable????Serializable????????????????????????????????????Externalizable???????
package com.hollischaung.serialization.ExternalizableDemos;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Created by hollis on 16/2/17.
* ???Externalizable???
*/
public class User1 implements Externalizable {
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;
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public void readExternal(ObjectInput in) throws IOException?? ClassNotFoundException {
}
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
"?? age=" + age +
'}';
}
}
package com.hollischaung.serialization.ExternalizableDemos;
import java.io.*;
/**
* Created by hollis on 16/2/17.
*/
public class ExternalizableDemo1 {
//????????????????????????????????????????????????????????????
//IOException??????
public static void main(String[] args) throws IOException?? ClassNotFoundException {
//Write Obj to file
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("tempFile"));
User1 user = new User1();
user.setName("hollis");
user.setAge(23);
oos.writeObject(user);
//Read Obj from file
File file = new File("tempFile");
ObjectInputStream ois =  new ObjectInputStream(new FileInputStream(file));
User1 newInstance = (User1) ois.readObject();
//output
System.out.println(newInstance);
}
}
//OutPut:
//User{name='null'?? age=0}
?????????????????????????User1????????л????????л???????????????????????????????????????????????????????????б???????????????Externalizable????Serializable????????
????Externalizable?????Serializable???y???ж?????????????????writeExternal()??readExternal()???????Externalizable????????????л??????л??????????????????дwriteExternal()??readExternal()???????????????????У???????????????????ж??????л???????????????????????????????????????????Externalizable???????л??????????????????????????л??????ι??????????????μ??????????????????????ε??????????????С?????????Externalizable???????????????public????ε????????
???????????????????????£?
package com.hollischaung.serialization.ExternalizableDemos;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Created by hollis on 16/2/17.
* ???Externalizable?????????writeExternal??readExternal????
*/
public class User2 implements Externalizable {
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;
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(name);
out.writeInt(age);
}
public void readExternal(ObjectInput in) throws IOException?? ClassNotFoundException {
name = (String) in.readObject();
age = in.readInt();
}
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
"?? age=" + age +
'}';
}
}
package com.hollischaung.serialization.ExternalizableDemos;
import java.io.*;
/**
* Created by hollis on 16/2/17.
*/
public class ExternalizableDemo2 {
//????????????????????????????????????????????????????????????
//IOException??????
public static void main(String[] args) throws IOException?? ClassNotFoundException {
//Write Obj to file
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("tempFile"));
User2 user = new User2();
user.setName("hollis");
user.setAge(23);
oos.writeObject(user);
//Read Obj from file
File file = new File("tempFile");
ObjectInputStream ois =  new ObjectInputStream(new FileInputStream(file));
User2 newInstance = (User2) ois.readObject();
//output
System.out.println(newInstance);
}
}
//OutPut:
//User{name='hollis'?? age=23}
??????Σ??????????????????????????
???????User?????????????????????????????????????java.io.InvalidClassException
????????Externalizable???????????ο????????
????ObjectOutput??ObjectInput ???
????ObjectInput??? ????? DataInput ????????????????????
????DataInput ????????????????ж???????????????? Java ?????????????????????????????? UTF-8 ??????????????? String ??????
????????????е?????????????????????????????????????????????????β (end of file)??????? EOFException??IOException ??????????????????????β??????????????????????????? IOException ?????? EOFException??????????????????????????£?????? IOException??
????ObjectOutput ??? DataOutput ?????????????д???????
????DataOutput ????????????????? Java ??????????????????????????Щ???д???????????????????????? String ????? UTF-8 ????????д????????????????????
?????????????д?????????з?????????????????????д?????????????? IOException??
????ObjectOutputStream???ObjectInputStream??
?????????????????????????????????????????ObjectOutputStream??writeObject???????????????г??????????ObjectInputStream??readObject??????洢?а????????????
???????????ObjectInputStream??ObjectOutputStream???????????????????????????????????Java?????л??????л????????????л?????Щ???
????Transient ?????
????Transient ???????????????????????л?????????????????ù??????????????????????л???????У?????????л???transient ???????????????????? int ????? 0??????????? null??????Transient ????????????????????????Java?????л??????л?
???????л?ID
????????????????????л??????????????·???????????????£???????????????????????????л? ID ?????£??? private static final long serialVersionUID)
???????л? ID ?? Eclipse ??????????????????????????? 1L???????????????????????? long ??????????????????? JDK ?????????????????????????飬?????????????????????? 1L ?????????????????????????????л????????????????????л? ID ?????????????Щ????????????л? ID ?????????????Щ???????á?