今天终于搞清了Serializable ,虽然不是自己写的程序!(去马赛克版)
2007-07-16 18:39
337 查看
类通过实现 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 来保存的。看一个列子: 最后结果如下:
node 0
node 1
node 2
node 3
node 4
node5
node 6
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
class tree implements java.io.Serializable ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public tree left;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public tree right;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int level;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static int count = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public tree(int depth) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
id = count++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
level = depth;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (depth > 0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
left = new tree(depth - 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
right = new tree(depth - 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void print(int levels) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < level; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.print(" ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(" node " + id);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (level <= levels && left != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
left.print(levels);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (level <= levels && right != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
right.print(levels);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String argv[]) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**//* 创建一个文件写入序列化树。 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileOutputStream ostream = new FileOutputStream(" tree.tmp ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**//* 创建输出流 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectOutputStream p = new ObjectOutputStream(ostream);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**//* 创建一个二层的树。 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
tree base = new tree(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p.writeObject(base); // 将树写入流中。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p.writeObject(" LiLy is 惠止南国 ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p.flush();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ostream.close(); // 关闭文件。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**//* 打开文件并设置成从中读取对象。 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileInputStream istream = new FileInputStream(" tree.tmp ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectInputStream q = new ObjectInputStream(istream);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**//* 读取树对象,以及所有子树 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
tree new_tree = (tree) q.readObject();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
new_tree.print(2); // 打印出树形结构的最上面 2级
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String name = (String) q.readObject();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(" " + name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (Exception ex) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ex.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
LiLy is 惠止南国
可以看到,在序列化的时候,writeObject与readObject之间的先后顺序。readObject将最先write的object read出来。用数据结构的术语来讲就姑且称之为先进先出吧!
在序列化时,有几点要注意的:
1:当一个对象被序列化时,只保存对象的非静态成员变量,不能保存任何的成员方法和静态的成员变量。
2:如果一个对象的成员变量是一个对象,那么这个对象的数据成员也会被保存。
3:如果一个可序列化的对象包含对某个不可序列化的对象的引用,那么整个序列化操作将会失败,并且会抛出一个NotSerializableException。我们可以将这个引用标记为transient,那么对象仍然可以序列化
还有我们对某个对象进行序列化时候,往往对整个对象全部序列化了,比如说类里有些数据比较敏感,不希望序列化,一个方法可以用transient来标识,另一个方法我们可以在类里重写
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void readObject(java.io.ObjectInputStream stream)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
throws IOException, ClassNotFoundException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void writeObject(java.io.ObjectOutputStream stream)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
throws IOException
这二个方法!
示例:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
class ObjectSerialTest ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Employee e1 = new Employee(" zhangsan ", 25, 3000.50);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Employee e2 = new Employee(" lisi ", 24, 3200.40);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Employee e3 = new Employee(" wangwu ", 27, 3800.55);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileOutputStream fos = new FileOutputStream(" employee.txt ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectOutputStream oos = new ObjectOutputStream(fos);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos.writeObject(e1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos.writeObject(e2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos.writeObject(e3);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileInputStream fis = new FileInputStream(" employee.txt ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectInputStream ois = new ObjectInputStream(fis);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Employee e;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (int i = 0; i < 3; i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e = (Employee) ois.readObject();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(e.name + " : " + e.age + " : " + e.salary);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
class Employee implements Serializable ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
double salary;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transient Thread t = new Thread();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Employee(String name, int age, double salary) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.age = age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.salary = salary;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private void writeObject(java.io.ObjectOutputStream oos) throws IOException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos.writeInt(age);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos.writeUTF(name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(" Write Object ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private void readObject(java.io.ObjectInputStream ois) throws IOException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
age = ois.readInt();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
name = ois.readUTF();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(" Read Object ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
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 来保存的。看一个列子: 最后结果如下:
node 0
node 1
node 2
node 3
node 4
node5
node 6
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
class tree implements java.io.Serializable ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public tree left;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public tree right;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int level;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private static int count = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public tree(int depth) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
id = count++;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
level = depth;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (depth > 0) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
left = new tree(depth - 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
right = new tree(depth - 1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void print(int levels) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < level; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.print(" ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(" node " + id);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (level <= levels && left != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
left.print(levels);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (level <= levels && right != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
right.print(levels);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String argv[]) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**//* 创建一个文件写入序列化树。 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileOutputStream ostream = new FileOutputStream(" tree.tmp ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**//* 创建输出流 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectOutputStream p = new ObjectOutputStream(ostream);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**//* 创建一个二层的树。 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
tree base = new tree(2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p.writeObject(base); // 将树写入流中。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p.writeObject(" LiLy is 惠止南国 ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p.flush();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ostream.close(); // 关闭文件。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**//* 打开文件并设置成从中读取对象。 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileInputStream istream = new FileInputStream(" tree.tmp ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectInputStream q = new ObjectInputStream(istream);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**//* 读取树对象,以及所有子树 */
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
tree new_tree = (tree) q.readObject();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
new_tree.print(2); // 打印出树形结构的最上面 2级
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String name = (String) q.readObject();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(" " + name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (Exception ex) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ex.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
LiLy is 惠止南国
可以看到,在序列化的时候,writeObject与readObject之间的先后顺序。readObject将最先write的object read出来。用数据结构的术语来讲就姑且称之为先进先出吧!
在序列化时,有几点要注意的:
1:当一个对象被序列化时,只保存对象的非静态成员变量,不能保存任何的成员方法和静态的成员变量。
2:如果一个对象的成员变量是一个对象,那么这个对象的数据成员也会被保存。
3:如果一个可序列化的对象包含对某个不可序列化的对象的引用,那么整个序列化操作将会失败,并且会抛出一个NotSerializableException。我们可以将这个引用标记为transient,那么对象仍然可以序列化
还有我们对某个对象进行序列化时候,往往对整个对象全部序列化了,比如说类里有些数据比较敏感,不希望序列化,一个方法可以用transient来标识,另一个方法我们可以在类里重写
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void readObject(java.io.ObjectInputStream stream)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
throws IOException, ClassNotFoundException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void writeObject(java.io.ObjectOutputStream stream)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
throws IOException
这二个方法!
示例:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
class ObjectSerialTest ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) throws Exception ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Employee e1 = new Employee(" zhangsan ", 25, 3000.50);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Employee e2 = new Employee(" lisi ", 24, 3200.40);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Employee e3 = new Employee(" wangwu ", 27, 3800.55);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileOutputStream fos = new FileOutputStream(" employee.txt ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectOutputStream oos = new ObjectOutputStream(fos);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos.writeObject(e1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos.writeObject(e2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos.writeObject(e3);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FileInputStream fis = new FileInputStream(" employee.txt ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ObjectInputStream ois = new ObjectInputStream(fis);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Employee e;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for (int i = 0; i < 3; i++) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e = (Employee) ois.readObject();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(e.name + " : " + e.age + " : " + e.salary);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
class Employee implements Serializable ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
double salary;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
transient Thread t = new Thread();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Employee(String name, int age, double salary) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.age = age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.salary = salary;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private void writeObject(java.io.ObjectOutputStream oos) throws IOException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos.writeInt(age);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
oos.writeUTF(name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(" Write Object ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
private void readObject(java.io.ObjectInputStream ois) throws IOException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
age = ois.readInt();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
name = ois.readUTF();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(" Read Object ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- 今天终于搞清了Serializable ,虽然不是自己写的程序!
- 今天终于自己作了一个Ajax程序,陶醉中
- 今天学习C#,在dos下用csc命令编译,提示“csc不是内部或外部命令,也不是可运行的程序... ”
- 程序员要明白写程序不是个自己用的
- 今天终于会用读入挂了,虽然看不懂是什么意思,挂一下吧
- 今天终于把那个俄罗斯方块程序搞定.
- 终于能在自己的mac笔记本,在自己的iphone上调试自己的程序了
- 关于读不是自己写的程序的问题
- C#初学者—— 终于写出了自己第一个比较满意的程序
- 单链表的建立程序 没想到自己用的是最笨的尾插法 不要忘记了p从第一个节点开始而不是从头结点开始计数,因为头结点数据值为空
- 炫耀下今天自己完成的程序11
- 发个自己想的解汉诺塔的程序,虽然比较初级~
- 网络兼职不是梦,今天终于实现了
- 今天终于申请到了自己的BLOGS,真高兴`
- 今天遭到鄙视了。突然感觉自己昏昏沉沉的过日子 究竟不是个事 自己得努力了!~!
- 今天终于开了自己的博客
- 凡事总有个第一次,今天开始记录自己的程序人生。
- 今天终于有了自己的blog
- 1.今天终于有了自己的Blog!
- 今天终于把程序运行起来了,高兴!