使用Externalizable實現序列化java
public interface Externalizable extends java.io.Serializable { /** * The object implements the writeExternal method to save its contents * by calling the methods of DataOutput for its primitive values or * calling the writeObject method of ObjectOutput for objects, strings, * and arrays. * * @serialData Overriding methods should use this tag to describe * the data layout of this Externalizable object. * List the sequence of element types and, if possible, * relate the element to a public/protected field and/or * method of this Externalizable class. * * @param out the stream to write the object to * @exception IOException Includes any I/O exceptions that may occur */ void writeExternal(ObjectOutput out) throws IOException; /** * The object implements the readExternal method to restore its * contents by calling the methods of DataInput for primitive * types and readObject for objects, strings and arrays. The * readExternal method must read the values in the same sequence * and with the same types as were written by writeExternal. * * @param in the stream to read data from in order to restore the object * @exception IOException if I/O errors occur * @exception ClassNotFoundException If the class for an object being * restored cannot be found. */ void readExternal(ObjectInput in) throws IOException, ClassNotFoundException; }
首先,咱們在序列化對象的時候,因爲這個類實現了Externalizable 接口,在writeExternal()方法裏定義了哪些屬性能夠序列化,ide
哪些不能夠序列化,因此,對象在通過這裏就把規定能被序列化的序列化保存文件,不能序列化的不處理,而後在反序列的時候自動調用readExternal()方法,根據序列順序挨個讀取進行反序列,並自動封裝成對象返回,而後在測試類接收,就完成了反序列測試
因此說Exterinable的是Serializable的一個擴展this
須要序列化的類Personspa
package com.serializable; import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import java.text.SimpleDateFormat; import java.util.Date; public class Person implements Externalizable { private static final long serialVersionUID = 2534579427612174172L; private String name; private Integer age; private transient String gender; public Person() { } public Person(String name, Integer age, String gender) { this.name = name; this.age = age; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public void writeExternal(ObjectOutput out) throws IOException { Date date=new Date(); out.writeObject(name); out.writeObject(age); out.writeObject(date); } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { name=(String) in.readObject(); age=(Integer) in.readObject(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date date=(Date)in.readObject(); System.out.println("反序列化後的日期爲:"+sdf.format(date)); } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + '}'; } }
序列化類rest
public class Client { /** * 序列化方法 * @throws IOException * @throws FileNotFoundException */ public void serializable(Person person) throws FileNotFoundException, IOException { ObjectOutputStream outputStream=new ObjectOutputStream(new FileOutputStream("temp2")); outputStream.writeObject(person); } /** * 反序列化的方法 * @throws IOException * @throws FileNotFoundException * @throws ClassNotFoundException */ public Person deSerializable() throws FileNotFoundException, IOException, ClassNotFoundException{ ObjectInputStream ois=new ObjectInputStream(new FileInputStream("temp2")); return (Person) ois.readObject(); } @Test public void test() throws Exception { Client client=new Client(); Person person=new Person("村長",20,"man"); System.out.println("爲序列化以前的相關數據以下:\n"+person.toString()); client.serializable(person); Person newPerson=client.deSerializable(); System.out.println("-------------------------------------------------------"); System.out.println("序列化以後的相關數據以下:\n"+newPerson.toString()); } }
1.java中的序列化時transient變量(這個關鍵字的做用就是告知JAVA我不能夠被序列化)和靜態變量不會被序列化orm
2.也是最應該注意的,若是你先序列化對象A後序列化B,那麼在反序列化的時候必定記着JAVA規定先讀到的對象 是先被序列化的對象,不要先接收對象B,那樣會報錯.尤爲在使用上面的Externalizable的時候必定要注意讀取 的前後順序對象
3.實現序列化接口的對象並不強制聲明惟一的serialVersionUID,是否聲明serialVersionUID對於對象序列化的向 上向下的兼容性有很大的影響接口