/** 1、请使用序列化和反序列化机制,完成学生信息管理系统。 系统打开时显示以下信息: 欢迎使用学生信息管理系统,请认真阅读以下使用说明: 请输入不同的功能编号来选择不同的功能: [1]查看学生列表 [2]保存学生 [3]删除学生 [4]查看某个学生详细信息 -------------------------------------------------------------------- 学生信息列表展示 学号 姓名 性别 ------------------------------------ 1 zhangsan 男 2 lisi 女 ..... -------------------------------------------------------------------- 查看某个学生详细信息 学号:1 姓名:张三 生日:1990-10-10 性别:男 邮箱:zhangsan@123.com --------------------------------------------------------------------- 删除学生时,需要让用户继续输入删除的学生编号,根据编号删除学生。 注意:请使用序列化和反序列化,以保证关闭之后,学生数据不丢失。 学生数据要存储到文件中。 */
//学生类
private String name; //学号通过put添加 private String birthday; private String sex; private String emile; public StudentS(){} public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getEmile() { return emile; } public void setEmile(String emile) { this.emile = emile; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", birthday='" + birthday + '\'' + ", sex='" + sex + '\'' + ", emile='" + emile + '\'' + '}'; } public StudentS(String name, String birthday, String sex, String emile) { this.name = name; this.birthday = birthday; this.sex = sex; this.emile = emile; } }
package com; import java.io.*; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.Set; //学生系统工具类 //完成以下功能 /* [1]查看学生列表 [2]保存学生 [3]删除学生 [4]查看某个学生详细信息 */ public class StudentSystem { //删除学生时,需要让用户继续输入删除的学生编号,根据编号删除学生。 // 注意:请使用序列化和反序列化,以保证关闭之后,学生数据不丢失。 // 学生数据要存储到文件中。 //*/ //在删除固定编号学生时,我们应该考虑使用什么集合,数组显然不行无法真正删除里面的数据,链表在遍历查询指定编号学生时 //效率太低 ,如使用map集合让其编号为key值便可直接通过key进行数据删除 //尽量做到方法的封装,序列化和反序列化都可以封装起来 //创建map集合 Map<Integer,StudentS> map= new HashMap<>(); //初始化 public static Map<Integer,StudentS> getMap() throws IOException, ClassNotFoundException { File file = new File("chapter/studentSystem01.txt"); ObjectInputStream obj = null; obj = new ObjectInputStream(new FileInputStream(file)); Object o = obj.readObject(); Map<Integer, StudentS> m = (Map<Integer, StudentS>) o; return m; } public void initialize (){ map.put(1,new StudentS("张三","2000-06-08","男","23778900@qq.cpm")); map.put(2,new StudentS("李四","2000-09-25","男","2322900@qq.cpm")); map.put(3,new StudentS("王五","2001-06-15","男","6358900@qq.cpm")); serialize(map); } //删除学生 public void removeMap(int x) throws IOException, ClassNotFoundException { File file=new File("chapter/studentSystem01.txt"); ObjectInputStream obj=new ObjectInputStream(new FileInputStream(file) ); Object o=obj.readObject(); Map<Integer,StudentS> m=(Map<Integer,StudentS>) o; if (m.containsKey(x)) { m.remove(x); System.out.println("删除成功"); file.delete(); serialize(m); }//如果集合key中存在x /* File file=new File("chapter/studentSystem01.txt"); if (getMap().containsKey(x)){ getMap().remove(x); file.delete(); serialize(getMap()); }*/ else System.out.println("没有找到此学生"); } //查看学生列表 public void check(){ System.out.println("--------------------------------"); System.out.println("学生信息列表展示"); System.out.println("学号"+" "+"姓名"+" "+"性别"); deserialization(); System.out.println("--------------------------------"); } //注意:在进行序列化前要先将文件清空,将数据反序列化出来后新建集合放入,否侧会出现数据添加失败 //保存添加学生,让用户自己输入信息 public void add() throws IOException, ClassNotFoundException { System.out.println("学生信息格式如下,请输入:"); Scanner x=new Scanner(System.in); System.out.println("Int id, String name, String birthday, String sex, String emile"); int index=x.nextInt(); StudentS stu = new StudentS(x.next(), x.next(), x.next(), x.next()); File file=new File("chapter/studentSystem01.txt"); // ObjectInputStream obj = null; //FileInputStream in=null; //in=new FileInputStream(file); ObjectInputStream obj=new ObjectInputStream(new FileInputStream(file) ); Object o=obj.readObject(); Map<Integer,StudentS> m=(Map<Integer,StudentS>) o; m.put(index,stu); file.delete(); System.out.println("添加成功"); serialize(m); } //查看某个学生详细信息 public void particular(int y){ if (map.containsKey(y)) { System.out.println(y+" "+map.get(y).toString()); } else System.out.println("没有编号为"+y+"这个学生"); } //序列化,将内存中数据存到硬盘中 public void serialize(Map<Integer,StudentS> l){ ObjectOutputStream on=null; try { on = new ObjectOutputStream(new FileOutputStream("chapter/studentSystem01.txt")); on.writeObject(l); on.flush(); } catch (IOException e) { e.printStackTrace(); }finally { try { if (on != null) { on.close(); } on.close(); } catch (IOException e) { e.printStackTrace(); } } } //反序列化,从硬盘中取到内存 public void deserialization(){ ObjectInputStream obj=null; try { obj=new ObjectInputStream(new FileInputStream("chapter/studentSystem01.txt")); Object o=obj.readObject(); Map<Integer,StudentS> map01=(Map<Integer,StudentS>) o; //将map集合全部转换成一个Set集合 Set<Map.Entry<Integer,StudentS>> set=map01.entrySet(); //遍历 for (Map.Entry<Integer,StudentS> x:set) { System.out.println(x.getKey()+" "+x.getValue().getName()+" "+x.getValue().getSex()); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { obj.close(); } catch (IOException e) { e.printStackTrace(); } } } }
package com; import java.io.IOException; //学生系统测试 public class StudentSystemTest { public static void main(String[] args) { StudentSystem s1=new StudentSystem(); // s1.initialize(); /* try { s1.add(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }*/ s1.check(); try { s1.removeMap(2); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } s1.check(); } }