Java I/O的笔记(5)

Object 流

直接将Object写入或者读出

 

   1:  import java.io.*;
   2:   
   3:  public class TestObjectIO {
   4:      public static void main(String args[]) throws Exception {
   5:          T t = new T();
   6:          t.k = 8;
   7:          FileOutputStream fos = new FileOutputStream("d:/share/java/io/testobjectio.dat");
   8:          ObjectOutputStream oos = new ObjectOutputStream(fos);
   9:        
oos.writeObject(t);
  10:          oos.flush();
  11:          oos.close();
  12:          
  13:          FileInputStream fis = new FileInputStream("d:/share/java/io/testobjectio.dat");
  14:          ObjectInputStream ois = new ObjectInputStream(fis);
  15:          T tReaded = (T)ois.readObject();
  16:          System.out.println(tReaded.i + " " + tReaded.j + " " + tReaded.d + " " + tReaded.k);
  17:          
  18:      }
  19:  }
  20:   
  21:  class T 
  22:      
implements Serializable // Serializable为标记型接口,因为没有定义的方法,标记这个类可以序列化。
  23:  {
  24:      int i = 10;
  25:      int j = 9;
  26:      double d = 2.3;
  27:      
transient
 int k = 15;   //相当于K是透明的,在序列化的时候不予考虑。
  28:  }

 

Externalizable接口:是Serializable的子接口。定义了2个方法。

未完待续…

posted @ 2012-04-23 12:38  bevin-H  阅读(216)  评论(0编辑  收藏  举报