Externalizable 接口的使用。

实例1:

package cn.vicky.test; import java.io.*; class Blip1 implements Externalizable { public Blip1() { System.out.println("Blip1 Constructor"); } public void writeExternal(ObjectOutput out) throws IOException { System.out.println("Blip1.writeExternal"); } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { System.out.println("Blip1.readExternal"); } } class Blip2 implements Externalizable { Blip2() { System.out.println("Blip2 Constructor"); } public void writeExternal(ObjectOutput out) throws IOException { System.out.println("Blip2.writeExternal"); } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { System.out.println("Blip2.readExternal"); } } public class Blips { public static void main(String[] args) throws IOException, ClassNotFoundException { System.out.println("Constucting objects:"); Blip1 b1 = new Blip1(); Blip2 b2 = new Blip2(); // 创建输出流 ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("Blips.out")); // 保存到临时文件夹 // ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("D://Blips.out")); System.out.println("saving objects;"); o.writeObject(b1); o.writeObject(b2); o.close(); // 创建输入流 ObjectInputStream in = new ObjectInputStream(new FileInputStream("Blips.out")); // 读取临时文件夹下的文件 // ObjectInputStream in = new ObjectInputStream(new FileInputStream("D://Blips.out")); System.out.println("Recovoring b1:"); b1 = (Blip1) in.readObject(); // System.out.println("Recovoring b2:"); // b2=(Blip2)in.readObject(); } }

 

 打印:

Constucting objects:
Blip1 Constructor
Blip2 Constructor
saving objects;
Blip1.writeExternal
Blip2.writeExternal
Recovoring b1:
Blip1 Constructor
Blip1.readExternal

 

说明,在实现了Externalizable接口后,writeObject函数将通过writeExternal去赋值对象。readObject函数将通过readExterna函数去为对象赋值。

 

 

 

 

实例2:

 

package cn.vicky.test; import java.io.Serializable; public class Person implements Serializable { private static final long serialVersionUID = 1L; private String username = "Vicky"; private int age = 22; public Person() { super(); } public Person(String username, int age) { super(); this.username = username; this.age = age; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }

 

package cn.vicky.test; import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; public class Person2 implements Externalizable { private static final long serialVersionUID = 1L; private String username = "Vicky"; private int age = 22; public Person2() { super(); } public Person2(String username, int age) { super(); this.username = username; this.age = age; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { username = (String) in.readObject(); age = in.read(); } public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(" - username : " + getUsername()); out.write(getAge() + 100); } }

 

package cn.vicky.test; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; public class AppTest { private void saveGame() { MyTest m = new MyTest(); if (m != null) { try { FileOutputStream ostream = new FileOutputStream("t.txt"); ObjectOutputStream p = new ObjectOutputStream(ostream); p.writeObject(m); // writeExternal()自动执行 p.flush(); ostream.close(); } catch (IOException ioe) { System.out.println("Error saving file:"); System.out.println(ioe.getMessage()); } } } private void loadGame() { try { FileInputStream instream = new FileInputStream("t.txt"); ObjectInputStream p = new ObjectInputStream(instream); MyTest m = (MyTest) p.readObject();// readExternal()自动执行 m.say(); instream.close(); // Person person = new Person(); ObjectOutput out = new ObjectOutputStream(new FileOutputStream("D://AppTest.out")); out.writeObject(person); ObjectInput in = new ObjectInputStream(new FileInputStream("D://AppTest.out")); person = (Person) in.readObject(); System.out.println(person.getUsername()); System.out.println(person.getAge()); // Person2 person2 = new Person2(); ObjectOutput out2 = new ObjectOutputStream(new FileOutputStream("D://AppTest2.out")); out2.writeObject(person2); ObjectInput in2 = new ObjectInputStream(new FileInputStream("D://AppTest2.out")); person2 = (Person2) in2.readObject(); System.out.println(person2.getUsername()); System.out.println(person2.getAge()); } catch (Exception e) { System.out.println("Error loading file:"); System.out.println(e.getMessage()); } } public static void main(String[] args) { new AppTest().saveGame(); new AppTest().loadGame(); } }

 

打印:

 

fanruijun
88
123
fanruijun : 88
Vicky
22
 - username : Vicky
122

 

实例3:

package cn.vicky.test; import java.io.Externalizable; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; public class MyTest implements Externalizable { private String letterstates = "fanruijun"; private int num = 0; public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(letterstates); out.writeObject(88); // 在序列化的数据最后加个88 out.write(123); } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { letterstates = (String) in.readObject(); System.out.println(letterstates); num = (Integer) in.readObject(); System.out.println(num); System.out.println(in.read()); // 重点。。。 } public void say(){ System.out.println(letterstates + " : " + num); } public static void main(String[] args) { try { MyTest myTest = new MyTest(); ObjectOutput out = new ObjectOutputStream(new FileOutputStream("D://MyTest.txt")); myTest.writeExternal(out); // ObjectInput in = new ObjectInputStream(new FileInputStream("D://MyTest.txt")); myTest.readExternal(in); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }

 

打印:

 

fanruijun
88
-1  // 重点

 

 

 

总结,比较直接实现Serializable,一个类的对象被实例化和读取后,所有的属性都保存,且不会改变。但实现Externalizable,可以重写类的持久化,可以再持久化时或者读取时重新为对象的属性赋值,添加,删除或者修改。。。。。。

 

你可能感兴趣的:(Java)