package local; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; /** * @function : * @author :jy * @company :万里网 * @date :2011-6-28 */ class Test { /** * @param args */ public static void main(String[] args) { try { FileOutputStream out = new FileOutputStream("object.log"); ObjectOutputStream o = new ObjectOutputStream(out);// 基本数据类型流 Employee e1 = new Employee(11, "jinwei"); Employee e2 = new Employee(11, "jinyong"); o.writeObject(e1);// 写入序列化对象 o.writeObject(e2); o.close(); FileInputStream in = new FileInputStream("object.log"); ObjectInputStream ins = new ObjectInputStream(in); for (int i = 0; i < 2; i++) { Employee e = (Employee) ins.readObject();// 读出序列化对象 System.out.println(e.name + ":" + e.age); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class Employee implements Serializable {// 序列化对象需要实现该空接口 public int age; public String name; public transient Thread th = new Thread();//transient屏蔽某个对象或变量的序列化 public Employee(int age, String name) { this.age = age; this.name = name; } protected void ObjectOutputStream() throws IOException, SecurityException { //自定义写入序列化对象 } protected void ObjectInputStream() throws IOException, SecurityException { //自定义读出序列化对象 } }