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(3); p.writeObject(base); // 将树写入流中。
p.flush();
ostream.close(); // 关闭文件。 /* 打开文件并设置成从中读取对象。 */
FileInputStream istream = new FileInputStream("tree.tmp");
ObjectInputStream q = new ObjectInputStream(istream); /* 读取树对象,以及所有子树 */
tree new_tree = (tree)q.readObject(); new_tree.print(3); // 打印出树形结构的最上面 3 级
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
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(3); p.writeObject(base); // 将树写入流中。
p.flush();
ostream.close(); // 关闭文件。 /* 打开文件并设置成从中读取对象。 */
FileInputStream istream = new FileInputStream("tree.tmp");
ObjectInputStream q = new ObjectInputStream(istream); /* 读取树对象,以及所有子树 */
tree new_tree = (tree)q.readObject(); new_tree.print(3); // 打印出树形结构的最上面 3 级
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
出处:中文Java技术网 Vector对象是一个很灵活的java数据结构,在实际编程中,有时需要我们将一个Vector对象传递给另一个Java程序并保持Vector的数据结构状态,这时,我们可以将需要传递的对象实现java.io.Serializable接口,序列化这个类,由于序列化本身就是允许一个对象在虚拟机之间传送(或者经过一段空间,如在RMI中;或者经过一段时间,比如数据流被保存到一个文件中)。 关于类的序列化请查阅相关资料,本篇不在叙述,下面使用一个简单的程序说明如何把一个Vector对象序列化并放到一个流中(为了测试方便,这里放到一个文件流中,如果换成一个套接字就可以把对象发送给远程请求者) 程序1:把一个Vector对象存储到一个testvector.obj文件里(模拟server socket处理)
import java.io.*;
import java.util.*;class TestVector implements java.io.Serializable{
private Vector vect=new Vector();
public void add(Object obj){
this.vect.add(obj);
}
public void print(){
System.out.println(this.vect);
}
public static void main(String[] args){
try {
/* 将对象写到一个文件里 */
FileOutputStream objfile = new FileOutputStream("testvector.obj");
/* 创建一个输出流 */
ObjectOutputStream p = new ObjectOutputStream(objfile);
/* 创建一个TestVector对象 */
TestVector tv =new TestVector();
/*给Vector写入几String个对象*/
tv.add("One");
tv.add("Two");
tv.add("Three");
p.writeObject(tv); // 把tv写入流
p.flush();
objfile.close(); // 关闭文件对象
} catch (Exception e) {
e.printStackTrace();
}
}
}
编译程序1,运行后,在当前目录生成一个testvector.obj文件,这个文件里存放了TestVector类的数据
程序2:从testvector.obj文件里获取TestVector对象(模拟socke客户端)
import java.io.*;
import java.util.*;class ReadVectorObj{
public static void main (String[] args){
try {
/* 打开文件,读取Vector 存放的对象数据 */
FileInputStream objfile = new FileInputStream("testvector.obj");
ObjectInputStream q = new ObjectInputStream(objfile);
/* 获取对象原始数据 */
TestVector myvector = (TestVector)q.readObject();
myvector.print();
} catch (Exception e) {
e.printStackTrace();
}
}
}
编译程序2,运行后,读取testvector.obj文件,根据内容构建出原始的TestVector类。 参考1:对象序列化知识
参考2:列表:
下面列出了被标记为serializable的类:(这些类的子类也都是可序列化的) java.lang.Character
java.lang.Boolean
java.lang.String
java.lang.StringBuffer
java.lang.Throwable-(包括所有Exception的子类)
java.lang.Number-(包括Integer、Long等)
java.util.Hashtable
java.util.Random
java.util.Vector-(包括Stack)
java.util.Date
java.util.BitSet
java.io.File
java.net.InetAddress
java.rmi.server.RemoteObject AWT类 基本数据类型数组 对象数组是可序列化的(尽管对象有可能不是可序列化的)