二级JAVA第六章辅导:Java串行化
1.序列化示例1
ObjectInputStream和ObjectOutputStream。
公共类序列化演示{
public static void main(String[]args){
//TODO自动生成的方法存根
try {
Vector list;
buffered reader reader = new buffered reader(new InputStreamReader(
system . in));
System.out.print("检查以前的序列化列表");
try {
file inputstream fin = new file inputstream(" list . out ");
ObjectInputStream oin =新的ObjectInputStream(fin);
try {
Object obj = oin . read Object();
list =(Vector)obj;
} catch(ClassCastException e){
//TODO:处理异常
list = new Vector();
} catch(ClassNotFoundException e){
list = new Vector();
}
fin . close();
} catch(file not found exception e){
//TODO:handle exception
list = new Vector();
}
for(;;){
system . out . println(" Menu:");
System.out.println("1..添加项目”);
System.out.println("2..删除项目”);
System.out.println("3..列表项”);
System.out.println("4..保存并退出”);
system . out . println(" Choice ");
String response = reader . readline();
int choice = integer . parse int(response);
switch(choice){
案例一:
system . out . print(" Enter item:");
String item = reader . readline();
list . add element(item);
0条评论