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

问题描述:由于序列化的对象,不能修改属性,序列化一旦保存到文件中就不可修改,并且也不能删除文件中序列化的对象。(这里可以将对象填入到集合或数组中,反序列化到内存,通过对集合的删除操作后,并将其写回文件,可以实现对象的删除)但依然无法真正意义上的删除。因此梦阳辰此次系统的书写,对于删除功能,和修改功能其实是没有实现的,只实现了,增加,查找,和遍历。注意这里没有采用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!");
            }
        }

    }
//Modify
    private 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();
        }

    }
//Delete
    private 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();
        }
    }
//Find
    private 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();
    }
//ViewAllInformation
    private 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();
        }

    }

//Add
    private 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;
    }

    @Override
    public 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学习资源”,“人工智能”等即可获取学习资源。
在这里插入图片描述

  • 2
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值