鱼C论坛

 找回密码
 立即注册
查看: 1188|回复: 0

如何利用序列化+Objectoutput实现控制台输入的信息永久保存到硬盘的文件上

[复制链接]
发表于 2022-12-19 11:55:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 holistic杀手 于 2022-12-20 14:31 编辑

在控制台录入的学生信息,在重新运行之后并没有写入到文件里,哪里有问题呢

  1. import java.io.*;
  2. import java.util.*;

  3. public class StudentInfo {

  4.     Map<Integer,Student> studentMap = new HashMap<>();
  5.     Scanner s = new Scanner(System.in);

  6.     public void init(){
  7.         Student s1 = new Student(1,"zhangsan","男","1990-10-12","zhangsan@123.com");
  8.         Student s2 = new Student(2,"lisi","男","1990-12-12","lisi@123.com");
  9.         Student s3 = new Student(3,"wangwu","男","1980-11-12","wangwu@123.com");

  10.         studentMap.put(s1.getNo(),s1);
  11.         studentMap.put(s2.getNo(),s2);
  12.         studentMap.put(s3.getNo(),s3);

  13.         serializable(studentMap);
  14.     }

  15.     //查看学生列表
  16.     /*
  17.     --------------------------------------------------------------------
  18.         学生信息列表展示
  19.         学号                        姓名                        性别
  20.         ------------------------------------
  21.         1                                zhangsan                男
  22.         2                                lisi                        女
  23.         .....
  24.         --------------------------------------------------------------------
  25.      */
  26.     public void checkAll(){
  27.         Map<Integer,Student> studentMap1=deserializable();
  28.         Set<Map.Entry<Integer,Student>> entrySet=studentMap1.entrySet();
  29.         System.out.println("------------------------学生信息展示页面------------------------------------");
  30.         System.out.println("学号\t\t\t姓名\t\t\t性别\t\t\t生日\t\t\t邮箱");
  31.         for (Map.Entry<Integer,Student> node:entrySet){
  32.             System.out.println(node.getValue());
  33.         }
  34.         System.out.println("---------------------------------------------------------------------------");

  35.     }

  36.     /*
  37.     [2]保存学生
  38.      */
  39.     public void addStudent(){
  40.         Map<Integer,Student> studentMap1=deserializable();
  41.         System.out.println("请输入学生的信息:");
  42.         System.out.print("学号:");
  43.         int no=s.nextInt();
  44.         System.out.print("姓名:");
  45.         String name=s.next();
  46.         System.out.print("性别:");
  47.         String sex=s.next();
  48.         System.out.print("生日:");
  49.         String brith=s.next();
  50.         System.out.print("邮箱:");
  51.         String mailAddress=s.next();
  52.         Student newStudent =new Student(no,name,sex,brith,mailAddress);
  53.         studentMap1.put(no,newStudent);
  54.         serializable(studentMap1);
  55.         System.out.println("添加"+name+"成功");
  56.         System.out.println("-------------------------");


  57.     }


  58.     public void serializable(Map<Integer,Student> studentMap){
  59.         ObjectOutputStream oos=null;
  60.         try {
  61.             oos = new ObjectOutputStream(new FileOutputStream("studentsInfo"));
  62.             oos.writeObject(studentMap);

  63.             oos.flush();
  64.         } catch (IOException e) {
  65.             e.printStackTrace();
  66.         }finally {
  67.             if (oos!=null){
  68.                 try {
  69.                     oos.close();
  70.                 } catch (IOException e) {
  71.                     e.printStackTrace();
  72.                 }
  73.             }
  74.         }
  75.     }


  76.     public  Map<Integer,Student>  deserializable() {
  77.         ObjectInputStream ois = null;
  78.         try {
  79.             ois = new ObjectInputStream(new FileInputStream("studentsInfo"));
  80.             Map<Integer, Student> studentMap1 = (Map<Integer, Student>) ois.readObject();

  81.             return studentMap1;
  82.         } catch (IOException e) {
  83.             e.printStackTrace();
  84.         } catch (ClassNotFoundException e) {
  85.             e.printStackTrace();
  86.         } finally {
  87.             if (ois != null) {
  88.                 try {
  89.                     ois.close();
  90.                 } catch (IOException e) {
  91.                     e.printStackTrace();
  92.                 }
  93.             }
  94.         }
  95.         return null;
  96.     }

  97.     /*
  98.     [3]删除学生
  99.      */
  100.     public void deleteStudent(){
  101.         Map<Integer,Student> studentMap1=deserializable();
  102.         System.out.print("请输入学生的学号:");
  103.         int no=s.nextInt();

  104.         if (no<0||no>studentMap1.size()){
  105.             System.out.println("输入的学号不存在!");
  106.         }else {
  107.             studentMap1.remove(no);
  108.             System.out.println("删除成功");
  109.             serializable(studentMap1);
  110.         }


  111.     }

  112.     public void checkStudent(){
  113.         Map<Integer,Student> studentMap1=deserializable();
  114.         System.out.print("请输入学生的学号:");
  115.         int no=s.nextInt();
  116.         if (no<0||no>studentMap1.size()){
  117.             System.out.println("输入的学号不存在!");
  118.         }else {
  119.             System.out.println(
  120.                     "-----------------------------------------------"+"\n"+
  121.                     "学号:"+studentMap1.get(no).getNo()+"\n"+
  122.                     "名字:"+studentMap1.get(no).getName()+"\n"+
  123.                     "性别:"+studentMap1.get(no).getSex()+"\n"+
  124.                     "生日:"+studentMap1.get(no).getBirth()+"\n"+
  125.                     "邮箱:"+studentMap1.get(no).getMailAdress()+"\n"+
  126.                             "-----------------------------------------------"
  127.             );
  128.         }

  129.     }


  130. }
复制代码


  1. import java.io.Serializable;
  2. import java.util.Objects;

  3. public class Student implements Serializable {

  4.     private final static long serialVersionUID=12L;

  5.     private int no;
  6.     private String name;
  7.     private String sex;
  8.     private String birth;
  9.     private String mailAdress;

  10.     public Student(int no, String name, String sex, String birth, String mailAdress) {
  11.         this.no = no;
  12.         this.name = name;
  13.         this.sex = sex;
  14.         this.birth = birth;
  15.         this.mailAdress = mailAdress;
  16.     }

  17.     public Student() {
  18.     }

  19.     public int getNo() {
  20.         return no;
  21.     }

  22.     public void setNo(int no) {
  23.         this.no = no;
  24.     }

  25.     public String getName() {
  26.         return name;
  27.     }

  28.     public void setName(String name) {
  29.         this.name = name;
  30.     }

  31.     public String getSex() {
  32.         return sex;
  33.     }

  34.     public void setSex(String sex) {
  35.         this.sex = sex;
  36.     }

  37.     public String getBirth() {
  38.         return birth;
  39.     }

  40.     public void setBirth(String birth) {
  41.         this.birth = birth;
  42.     }

  43.     public String getMailAdress() {
  44.         return mailAdress;
  45.     }

  46.     public void setMailAdress(String mailAdress) {
  47.         this.mailAdress = mailAdress;
  48.     }

  49.     @Override
  50.     public String toString() {
  51.         //return  no+"\t\t\t"+name+"\t\t\t"+sex+"\t\t\t"+birth+"\t\t\t"+mailAdress;
  52.         return no+"\t\t\t"+String.format("%-10s",name)
  53.                 +String.format("%12s",sex)+String.format("%20s",birth)
  54.                 +String.format("%20s",mailAdress);
  55.     }

  56.     @Override
  57.     public boolean equals(Object o) {
  58.         if (this == o) return true;
  59.         if (o == null || getClass() != o.getClass()) return false;
  60.         Student student = (Student) o;
  61.         return no == student.no &&
  62.                 Objects.equals(name, student.name) &&
  63.                 Objects.equals(sex, student.sex) &&
  64.                 Objects.equals(birth, student.birth) &&
  65.                 Objects.equals(mailAdress, student.mailAdress);
  66.     }

  67.     @Override
  68.     public int hashCode() {
  69.         return Objects.hash(no, name, sex, birth, mailAdress);
  70.     }
  71. }
复制代码

  1. import java.util.Map;
  2. import java.util.Scanner;
  3. import java.util.Set;

  4. public class StudentSystemTest {
  5.     public static void main(String[] args) {
  6.         StudentInfo si =new StudentInfo();

  7.         //si.init();

  8.         System.out.println("欢迎使用学生信息管理系统,请认真阅读以下使用说明:");
  9.         while (true){
  10.             System.out.println("请输入不同的功能编号来选择不同的功能:");
  11.             System.out.println("[1]查看学生列表");
  12.             System.out.println("[2]保存学生");
  13.             System.out.println("[3]删除学生");
  14.             System.out.println("[4]查看某个学生详细信息");
  15.             System.out.println("[0]退出系统");
  16.             Scanner s= new Scanner(System.in);
  17.             int n=s.nextInt();
  18.             if (n==1){
  19.                 si.checkAll();
  20.             }else if (n==2) {
  21.                 si.addStudent();
  22.             }else if (n==3){
  23.                 si.deleteStudent();
  24.             }else if (n==4){
  25.                 si.checkStudent();
  26.             }else if (n==0){
  27.                 System.out.println("感谢您的使用~");
  28.                 return;
  29.             }else{
  30.                 System.out.println("输入的功能编号有误!");
  31.             }

  32.         }
  33.     }
  34. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 00:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表