![]() |
|
||||||||||||||
| | 网站首页 | 数据库教程 | web编程 | 服务器 | 程序设计 | | ||
|
||
|
||||||
| 今天终于搞清了Serializable ,虽然不是自己写的程序!(去马赛克版) | ||||||
作者:佚名 文章来源:不详 点击数: 更新时间:2007-9-2 ![]() |
||||||
|
类通过实现 java.io.Serializable 接口以启用其序列化功能。
Java的"对象序列化"能让你将一个实现了Serializable接口的对象转换成一组byte,这样日后要用这个对象时候,你就能把这些byte数据恢复出来,并据此重新构建那个对象了。 要想序列化对象,你必须先创建一个OutputStream,然后把它嵌进ObjectOutputStream。这时,你就能用writeObject( )方法把对象写入OutputStream了。 writeObject 方法负责写入特定类的对象的状态,以便相应的 readObject 方法可以还原它。通过调用 out.defaultWriteObject 可以调用保存 Object 的字段的默认机制。该方法本身不需要涉及属于其超类或子类的状态。状态是通过使用 writeObject 方法或使用 DataOutput 支持的用于基本数据类型的方法将各个字段写入 ObjectOutputStream 来保存的。 读的时候,你得把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(" " + name);![]() } catch (Exception ex) ...{ ex.printStackTrace(); } } }
LiLy is 惠止南国 可以看到,在序列化的时候,writeObject与readObject之间的先后顺序。readObject将最先write的object read出来。用数据结构的术语来讲就姑且称之为先进先出吧! 在序列化时,有几点要注意的: 还有我们对某个对象进行序列化时候,往往对整个对象全部序列化了,比如说类里有些数据比较敏感,不希望序列化,一个方法可以用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); } } }![]() ![]() 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 "); } }本文来源:http://blog.csdn.net/yangm1203/archive/2007/07/16/1693931.aspx
|
||||||
| 文章录入:admin 责任编辑:admin | ||||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | ||||||
| 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 网站地图 | 管理登录 | | |||
|