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(); // 关闭文件对象
FileInputStream objfile = new FileInputStream("testvector.obj");
ObjectInputStream q = new ObjectInputStream(objfile);
/* 获取对象原始数据 */
TestVector myvector = (TestVector) q.readObject();
myvector.print();
}
catch (Exception e) {
e.printStackTrace();
}
}
}