0%

对象流改造集合

HashMap与对象流组合应用(主函数部分实现)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public static void main(String[] args) throws Exception{
//用File可判断文件是否为空,否则出现异常
File file = new File("X:\\xxx.xxx");
long len = file.length();
Map<String, Person> map = null;
if(len>0){//有数据
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Map<String, Person> map = (Map<String, Person>)ois.readObject();
}else{
map = new HashMap<String, Person>();
}

do{
System.out.println("************请输入如下选项************");
System.out.println("\t\t\t1.信息录入");
System.out.println("\t\t\t2.信息查询");
System.out.println("\t\t\t3.信息修改");
System.out.println("\t\t\t4.信息删除");
System.out.println("\t\t\t5.退出");
Scanner input = new Scanner(System.in);
int flag = input.nextInt();
switch(flag){
case 1:
Operation.addPerson(input,map);
break;
case 2:
Operation.getAllPerson(map);
break;
case 3:
break;
case 4:
break;
case 5:
System.out.println("退出成功");
FileOutputStream fos = new FileOutputStream("X:\\xxx.xxx");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(map);
oos.close();
fos.close();
System.exit(0);
break;
}
}while(true);
}
------ THEEND ------

欢迎关注我的其它发布渠道