问题描述:由于序列化的对象,不能修改属性,序列化一旦保存到文件中就不可修改,并且也不能删除文件中序列化的对象。(这里可以将对象填入到集合或数组中,反序列化到内存,通过对集合的删除操作后,并将其写回文件,可以实现对象的删除)但依然无法真正意义上的删除。因此梦阳辰此次系统的书写,对于删除功能,和修改功能其实是没有实现的,只实现了,增加,查找,和遍历。注意这里没有采用new File的形式,需要自己创建文件。
对于反序列化输出多个对象存在的问题也许这篇文章能帮到你:
Java序列化及反序列化将多个对象追加到文件并读取多个对象(ObjectOutputStream,ObjectInputStream)

  • 用户类:

//说明:注释代码为new了多次ObjectOutputStream的解决方案。
public class Teachers {public static void main(String[] args) {Scanner sc = new Scanner(System.in);ObjectOutputStream os = null;FileOutputStream fileOut=null;try {fileOut=new FileOutputStream("src/Day32/StudentsInformation",true);os =new ObjectOutputStream(fileOut);} catch (IOException e) {e.printStackTrace();}while(true){System.out.println("========请选择你要进行的操作编号==========");System.out.println("\t\t\t添加学生信息(1)\n\t\t\t浏览所有学生信息(2)");System.out.println("\t\t\t查找学生信息(3)\n\t\t\t删除学生信息(4)");System.out.println("\t\t\t修改学生信息(5)\n\t\t\t退出系统(-1)");System.out.println("==========================================\n请选择功能:");int flag = sc.nextInt();switch (flag){case 1:AddInformation(sc,os);break;case 2:try {ViewInformation();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}break;case 3:try {FindInformation(sc);} catch (MyException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}break;case 4:try {DeleteInformation(sc);} catch (MyException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}break;case 5:try {MoidfyInformation(sc);} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}break;case -1:sc.close();try {os.close();//退出系统时关闭os流} catch (IOException e) {e.printStackTrace();}return;default:System.out.println("The number you entered is wrong,please enter again!");}}}
//Modifyprivate static void MoidfyInformation(Scanner sc) throws IOException, ClassNotFoundException {ObjectInputStream ois =null;FileInputStream out =new FileInputStream("src/Day32/StudentsInformation") ;try {//反序列化ois = new ObjectInputStream(out);} catch (IOException e) {e.printStackTrace();}Students p=null;System.out.println("请输入你要修改的学生学号:");int num = sc.nextInt();while(out.available()>0){//代表文件还有内容p=(Students)ois.readObject();if (p.getNum() == num) {System.out.println("请输入新的名字:");String name =sc.next();p.setName(name);System.out.println("请输入新的学号:");int num1 = sc.nextInt();p.setNum(num1);System.out.println("请输入性别:");String gender = sc.next();p.setGender(gender);System.out.println("请输入生日:(如:1999-9-10)");String birthday =sc.next();p.setBirthday(birthday);System.out.println("请输入联系方式:");String contactInformation =sc.next();p.setContactInformation(contactInformation);System.out.println("修改成功!");}}if(ois !=null){ois.close();}}
//Deleteprivate static void DeleteInformation(Scanner sc) throws MyException, IOException, ClassNotFoundException {ObjectInputStream ois =null;FileInputStream out =new FileInputStream("src/Day32/StudentsInformation") ;try {//反序列化ois = new ObjectInputStream(out);} catch (IOException e) {e.printStackTrace();}Students p=null;System.out.println("请输入你要删除的学生学号:");int num = sc.nextInt();while(out.available()>0){//代表文件还有内容p=(Students)ois.readObject();if (p.getNum() == num) {p.setName(null);p.setNum(0);p.setGender(null);p.setBirthday(null);p.setContactInformation(null);System.out.println("删除成功!");}}if(ois!=null){ois.close();}}
//Findprivate static void FindInformation(Scanner sc,int...args) throws MyException, IOException, ClassNotFoundException {//可变长度参数ObjectInputStream ois = null;FileInputStream out = new FileInputStream("src/Day32/StudentsInformation");try {//反序列化ois = new ObjectInputStream(out);} catch (IOException e) {e.printStackTrace();}Students p = null;System.out.println("请输入你要查找的学生学号:");int num = sc.nextInt();while (out.available() > 0) {//代表文件还有内容p = (Students) ois.readObject();if (num == p.getNum()) {System.out.println(p);}}ois.close();}
//ViewAllInformationprivate static void ViewInformation() throws IOException, ClassNotFoundException {ObjectInputStream ois =null;FileInputStream out =new FileInputStream("src/Day32/StudentsInformation") ;try {//反序列化ois = new ObjectInputStream(out);} catch (IOException e) {e.printStackTrace();}Students p=null;/* p=(Students)ois.readObject();//从流中读取对象,需要进行强转System.out.println(p);*/while(out.available()>0){//代表文件还有内容/*byte[] buf =new byte[4];out.read(buf);//取走头部,不做处理*/p=(Students)ois.readObject();   //读取第二个对象System.out.println(p);}try {ois.close();} catch (IOException e) {e.printStackTrace();}}//Addprivate static void AddInformation(Scanner sc,ObjectOutputStream os) {/* ObjectOutputStream os = null;FileOutputStream fileOut=null;try {fileOut=new FileOutputStream("src/Day32/StudentsInformation",true);//序列化,只能实例化一个对象流,不然添加的时候就保存到不同的地方了os =new ObjectOutputStream(fileOut);} catch (IOException e) {e.printStackTrace();}*/System.out.println("请输入名字:");String name =sc.next();System.out.println("请输入学号:");int num = sc.nextInt();//如果要添加的学号学号存在,抛异常,提醒用户try {IsExist(num);} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (MyException e) {e.printStackTrace();return;//出现异常,退出方法}System.out.println("请输入性别:");String gender = sc.next();System.out.println("请输入生日:(如:1999-9-10)");String birthday =sc.next();System.out.println("请输入联系方式:");String contactInformation =sc.next();Students a =new Students(name,num,gender,birthday,contactInformation);try {os.writeObject(a);os.flush();} catch (IOException e) {e.printStackTrace();}/*finally {if(os!=null){try {os.close();} catch (IOException e) {e.printStackTrace();}}}*/}//判断是否已经存在该学号private static void IsExist(int num) throws IOException, ClassNotFoundException, MyException {ObjectInputStream ois = null;FileInputStream out = new FileInputStream("src/Day32/StudentsInformation");try {//反序列化ois = new ObjectInputStream(out);} catch (IOException e) {e.printStackTrace();}Students p = null;while (out.available() > 0) {//代表文件还有内容p = (Students) ois.readObject();if (num == p.getNum()) {throw new MyException("学号已存在!");//注意抛了异常就不会执行下面代码了,所以不用return}}ois.close();}
}
  • 学生类:

public class Students implements Serializable {private static final long serizlVersionUID=646646465464646546L;private  String name;private int num;//学号private  String gender;//性别private String birthday;//生日private String ContactInformation;//联系方法//构造方法public Students() {}public Students(String name, int num, String gender, String birthday, String contactInformation) {this.name = name;this.num = num;this.gender = gender;this.birthday = birthday;ContactInformation = contactInformation;}public static long getSerizlVersionUID() {return serizlVersionUID;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getBirthday() {return birthday;}public void setBirthday(String birthday) {this.birthday = birthday;}public String getContactInformation() {return ContactInformation;}public void setContactInformation(String contactInformation) {ContactInformation = contactInformation;}@Overridepublic String toString() {return "Students{" +"name='" + name + '\'' +", num=" + num +", gender='" + gender + '\'' +", birthday=" + birthday +", ContactInformation='" + ContactInformation + '\'' +'}';}
}
  • 自定义异常类:

public class MyException extends Exception {public MyException() {}public MyException(String message) {super(message);}
}

关注公众号【轻松玩编程】回复关键字激活码即可获取:全家桶通用激活码(激活码支持idea,pycharm,webstorm,phpstorm…激活)定期更新。适用于最新及以下版本。(无需破解和修改hosts,如果破解过请卸载重新安装官方原版,如果修改过hosts,请删除你添加的网址,然后重新激活)

回复关键字“电子书”,“计算机资源”,“Java从入门到进阶”,”JavaScript教程“,“算法”,“Python学习资源”,“人工智能”等即可获取学习资源。

学生信息管理系统(序列化与反序列化实现)相关推荐

  1. (解析+源码)基于JAVA Swing+MySQL实现学生信息管理系统(增、删、改、查)数据库/文件存储

    根据学校对学生信息日常管理需要,学生信息管理系统包括以下功能: 登录系统: 新建学生信息:添加学生信息: 删除学生信息:对指定学生信息进行删除: 修改学生信息:对指定学生信息进行修改 查找学生信息:输 ...

  2. 基于swing+awt学生信息管理系统

    源码编号:F-A02 项目类型:Java SE项目(awt+swing)开源免费 项目名称:基于swing+awt学生信息管理系统(manager) 当前版本:V1.0.1版本 主要技术:java.a ...

  3. Java 学生信息管理系统 (mysql版)

    引言:如果需要定制类似的图形界面版的Java 管理系统,比如控制台版的,Java web版的,ssm版,开发工具为idea和eclipse.myEclipse的,提供远程服务,需要源码,或者需要项目实 ...

  4. 【VB】学生信息管理系统5——数据库代码

    这次学生信息管理系统在代码的理解过程中遇到了一些问题.总结如下: 1. sql server的安装过程各个步骤的意思.在安装SQL Server的时候按照网上的步骤,我觉得这个需要学完整个数据库再返回 ...

  5. 【VB】学生信息管理系统4——数据库的发展

    由于连接数据的时候出现了很多不懂得问题,为什么要连接,它是怎么连接的,查着查着,就越看越多.又不舍得就这么放过这些问题,所以就耐心看看究竟是怎么回事! 1.自从出现数据库,人们渴望用数据和应用程序做交 ...

  6. 【VB】学生信息管理系统3——连接数据库的前提

    在窗体设计和编写程序代码后,需要进行数据库的连接. 大概要做的:下载安装数据库--配置数据源ODBC--测试连接数据库--进行数据库中内容的添加. 关键是这些我都不会,这才是重点! 在学习数据库的过程 ...

  7. 【VB】学生信息管理系统2——窗体设计

    这次学生系统是照着书敲的,先敲完然后开始调试!中途遇到了很多问题,查了很多,这里不容易系统的总结!所以就针对各个问题,各个击破! 问题一:VB 6.0中,状态栏控件(sbstatusbar):右击选项 ...

  8. 【VB】学生信息管理系统1——系统设计怎样开始?

    历时两周完成用VB完成的学生信息管理系统.从刚开始只会敲好玩的小程序到现在完整的做出一个像样的系统.自己的编程思维进行了很大的跨越. 这次的学生信息管理系统让我从整体的角度看到了一个系统设计的过程. ...

  9. [置顶]完美简版学生信息管理系统(附有源码)管理系统

    简版学生信息管理系统 目前为止找到的简版系统中最新.最全的java类管理系统 点击进入简版系统 如果无法直接连接,请进入: https://blog.csdn.net/weixin_43419816/ ...

  10. c语言饭卡管理系统链表文件,C语言《学生信息管理系统》链表+文件操作

    今天带来的是一个链表版本的<学生信息管理系统>,功能包括:添加.显示.查询.删除.保存.读取,等功能模块,链表是C语言的进阶内容,希望大家好好学习,这里的代码可能会有一些瑕疵,希望大家提供 ...

最新文章

  1. Zookeeper集群 + Kafka集群 + KafkaOffsetMonitor 监控
  2. 如何开发一个属于自己的小程序
  3. python开发闹钟_「玩转树莓派」为女朋友打造一款智能语音闹钟
  4. 关于企业应用架构中前置机的作用
  5. python3 拼接字符串的7种方法
  6. Intel Realsense D435 奇怪的现象记录:帧卡住,但wait_for_frame()不报错
  7. java 排名相同_Java程序员十年面试经验,助你成为offer收割机
  8. jvm 启动参数设置
  9. memcpy、memmove、memset、memchr、memcmp、strstr详解
  10. 《并行计算的编程模型》一3.8.3 原子交换和条件交换
  11. activiti7 和业务_华电集团电子商务平台非招标业务操作指南
  12. Audio播放流程(一)---MediaPlayer流程
  13. 智能车学习(十四)——K60单片机GPIO学习
  14. java jconsole 远程连接_jconsole连接远程tomcat
  15. 知识图谱顶刊综述 - (2021年4月) A Survey on Knowledge Graphs: Representation, Acquisition, and Applications
  16. 7-163 判断正整数n是否同时含有奇数和偶数
  17. 解决:Data truncation: Data too long for column ‘XXX‘ at row 1
  18. Android初级基础知识复习(十八) —— 自定义通知栏
  19. c语言程序输入自己班级学号,找高手帮忙c语言程序::输入一个班10个学生的学号和每个学生考试三门功课(数学、英语、计算机基础)的成绩...
  20. IOS Appstore 预览图尺寸

热门文章

  1. Macbook air M1 关闭SIP;更改安全策略
  2. 解决 Android N 上报错:android.os.FileUriExposedException: file:///storage/emulated/0/
  3. 计算机教师线下研修方式与内容,工作坊线下研修活动心得体会
  4. 长篇文章根据文章H标签自动生成导航目录方法
  5. PostgreSQL与MySQL开源协议对比
  6. ElementUI:Checkbox实现单选,嵌套多选
  7. 用Fragment实现图片简易浏览
  8. 运行JSP页面显示 404
  9. OFFICE2013 打开两个word文档卡死的解决办法
  10. 优化器统计信息_高水位_柱状图等