Serialize an object to a file
Suppose we have a class called Queue.class. We want to save the state of the Queue in a file. Since our Queue extends the Vector class, the methods needed to serialize the object are already done. All we need is an input or output stream. 
First the Queue class  import java.util.Vector;
 import java.io.*; public class Queue extends Vector {
  /*
  ** FIFO, first in first out
  */
  Queue() {
  super();
  }
 
 void put(Object o) {
  addElement(o);
  } Object get() {
  if (isEmpty()) return null;
    Object o = firstElement();
    removeElement(o);
    return o;
  } Object peek() {
  if (isEmpty()) return null;
    return firstElement();
    }
}
 To serialize (save the Queue state to a file) :  public static void main(String args[]) {
  Queue theQueue;
  
  theQueue = new Queue();
  theQueue.put("element 1");
  theQueue.put("element 2");
  theQueue.put("element 3");
  theQueue.put("element 4");
  System.out.println(theQueue.toString());
  
  // serialize the Queue
  System.out.println("serializing theQueue");
  try {
      FileOutputStream fout = new FileOutputStream("thequeue.dat");
      ObjectOutputStream oos = new ObjectOutputStream(fout);
      oos.writeObject(theQueue);
      oos.close();
      }
   catch (Exception e) { e.printStackTrace(); }
  }
 
To unserialize (to load a previously saved Queue) :   public static void main(String args[]) {
   Queue theQueue;
    
   theQueue = new Queue();
    
   // unserialize the Queue
   System.out.println("unserializing theQueue");
   try {
    FileInputStream fin = new FileInputStream("thequeue.dat");
    ObjectInputStream ois = new ObjectInputStream(fin);
    theQueue = (Queue) ois.readObject();
    ois.close();
    }
   catch (Exception e) { e.printStackTrace(); }
     
   System.out.println(theQueue.toString());     
   
 

解决方案 »

  1.   

    很基本的东西要掌握.
    class A implements Serializable
    {
       ...;
    }
    A 就可以序列化了.不要实现什么方法.write
    OutputStream out = getOutStream();//可以是socket,文件等IO流
    out.writeObject(a); //就可以write,否则就出NotSerializableException
    存的时候是递归的存所有成员变量.read
    InputStream in = getInputStream();
    a = in.readObject(a);当然,一个大的类有时不需要存所有的成员变量,有两个方法解决
    (1)在不需要序列话的成员变量前加transient关键字.
    (2)Class A 不实现Serializable,实现Externalizable,能实现最大的控制权
       自己来写  writeExternal ,readExternal
    我在我的项目里全用Externalizable,自由度大.该存的存,不该存的不存.
    Serialize相关的概念就是Stream,说起来就麻烦了,说实话有时间发贴子,还不如看书,看jdk source很有收获的.
      

  2.   

        楼上的说法有些问题。        如果类中的所有的成员变量的类型都已实现了Serializable接口(例如成员变量的类型是LinkedList,Vector等),那么该类只要实现简单的加上"implements Serializable"就可以序列化了;    但是如果成员变量的类型没有实现Serializable接口,上面的做法就会出问题。    
      

  3.   

    skyyoung(路人甲) 
    太谢谢了!!!外层该有个类实现Serialize类吧,你看,对么?
    public class TestSerializable implements Serializable {
      public static void main(String args[]) {
      Queue theQueue;
      
      theQueue = new Queue();
      theQueue.put("element 1");
      theQueue.put("element 2");
      theQueue.put("element 3");
      theQueue.put("element 4");
      System.out.println(theQueue.toString());
      
      // serialize the Queue
      System.out.println("serializing theQueue");
      try {
          FileOutputStream fout = new FileOutputStream("thequeue.dat");
          ObjectOutputStream oos = new ObjectOutputStream(fout);
          oos.writeObject(theQueue);
          oos.close();
          }
      catch (Exception e) { e.printStackTrace(); }
      }}
      

  4.   

    iloveyouonlyonce(唱首情歌麻一麻,吞杯毒酒辣一辣) 下午请我吃饭