读的时候,你得把InputStream嵌到ObjectInputStream里面,然后再调用readObject( )方法。不过这样读出来的,只是一个Object的reference,因此在用之前,还得先下传。readObject 方法负责从流中读取并还原类字段。它可以调用 in.defaultReadObject 来调用默认机制,以还原对象的非静态和非瞬态字段。 defaultReadObject 方法使用流中的信息来分配流中通过当前对象中相应命名字段保存的对象的字段。这用于处理类发展后需要添加新字段的情形。该方法本身不需要涉及属于其超类或子类的状态。状态是通过使用 writeObject 方法或使用 DataOutput 支持的用于基本数据类型的方法将各个字段写入 ObjectOutputStream 来保存的。 看一个列子:
import java.io. * ; class tree implements java.io.Serializable { public tree left; public tree right; public int id; public int level; private static int count = 0 ; public tree( int depth) { id = count ++ ; level = depth; if (depth > 0 ) { left = new tree(depth - 1 ); right = new tree(depth - 1 ); } } public void print( int levels) { for ( int i = 0 ; i < level; i ++ ) System.out.print( " " ); System.out.println( " node " + id); if (level <= levels && left != null ) left.print(levels); if (level <= levels && right != null ) right.print(levels); } public static void main (String argv[]) { try { /**/ /* 创建一个文件写入序列化树。 */ FileOutputStream ostream = new FileOutputStream( " tree.tmp " ); /**/ /* 创建输出流 */ ObjectOutputStream p = new ObjectOutputStream(ostream); /**/ /* 创建一个二层的树。 */ tree base = new tree( 2 ); p.writeObject(base); // 将树写入流中。 p.writeObject( " LiLy is 惠止南国 " ); p.flush(); ostream.close(); // 关闭文件。 /**/ /* 打开文件并设置成从中读取对象。 */ FileInputStream istream = new FileInputStream( " tree.tmp " ); ObjectInputStream q = new ObjectInputStream(istream); /**/ /* 读取树对象,以及所有子树 */ tree new_tree = (tree)q.readObject(); new_tree.print( 2 ); // 打印出树形结构的最上面 2级 String name = (String)q.readObject(); System.out.println( " /n " + name); } catch (Exception ex) { ex.printStackTrace(); } } }
最后结果如下: node 0 node 1node 2node 3 node 4node 5node 6
LiLy is 惠止南国 可以看到,在序列化的时候,writeObject与readObject之间的先后顺序。readObject将最先write的object read出来。用数据结构的术语来讲就姑且称之为先进先出吧! 在序列化时,有几点要注意的: 1:当一个对象被序列化时,只保存对象的非静态成员变量,不能保存任何的成员方法和静态的成员变量。 2:如果一个对象的成员变量是一个对象,那么这个对象的数据成员也会被保存。 3:如果一个可序列化的对象包含对某个不可序列化的对象的引用,那么整个序列化操作将会失败,并且会抛出一个NotSerializableException。我们可以将这个引用标记为transient,那么对象仍然可以序列化 还有我们对某个对象进行序列化时候,往往对整个对象全部序列化了,比如说类里有些数据比较敏感,不希望序列化,一个方法可以用transient来标识,另一个方法我们可以在类里重写
private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException; private void writeObject(java.io.ObjectOutputStream stream) throws IOException这二个方法! 示例:
import java.io. * ; class ObjectSerialTest { public static void main(String[] args) throws Exception { Employee e1 = new Employee( " zhangsan " , 25 , 3000.50 ); Employee e2 = new Employee( " lisi " , 24 , 3200.40 ); Employee e3 = new Employee( " wangwu " , 27 , 3800.55 ); FileOutputStream fos = new FileOutputStream( " employee.txt " ); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(e1); oos.writeObject(e2); oos.writeObject(e3); oos.close(); FileInputStream fis = new FileInputStream( " employee.txt " ); ObjectInputStream ois = new ObjectInputStream(fis); Employee e; for ( int i = 0 ;i < 3 ;i ++ ) { e = (Employee)ois.readObject(); System.out.println(e.name + " : " + e.age + " : " + e.salary); } ois.close(); } } class Employee implements Serializable { String name; int age; double salary; transient Thread t = new Thread(); public Employee(String name, int age, double salary) { this .name = name; this .age = age; this .salary = salary; } private void writeObject(java.io.ObjectOutputStream oos) throws IOException { oos.writeInt(age); oos.writeUTF(name); System.out.println( " Write Object " ); } private void readObject(java.io.ObjectInputStream ois) throws IOException { age = ois.readInt(); name = ois.readUTF(); System.out.println( " Read Object " ); } }