日期:2014-05-20  浏览次数:20854 次

静态能被序列化??
package objectstream;
import testclass.Person;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
class ObjectStreamDemo{
        
        public static void writeObj()throws IOException{
          ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("Person.txt"));//创建写入指定 OutputStream 的 ObjectOutputStream。
                                                                                                                                                                                //此构造方法将序列化流部分写入底层流
        
          oos.writeObject(new Person("zhang",12,"haha"));//将指定的对象写入 ObjectOutputStream
          /*
          对象的类、类的签名(SerialVersionUID),以及类及其所有父类的非瞬态和非静态字段的值都将被写入
          */
          
          oos.close();
        }
        
        public static void readObj()throws IOException,ClassNotFoundException{
          ObjectInputStream ois=new ObjectInputStream(new FileInputStream("Person.txt"));//创建从指定 InputStream 读取的 ObjectInputStream。
          
          Person p=(Person)ois.readObject();//将读取流中的一个对象返回,当有多个对象可以采用循环读取