如何利用序列化+Objectoutput实现控制台输入的信息永久保存到硬盘的文件上
本帖最后由 holistic杀手 于 2022-12-20 14:31 编辑在控制台录入的学生信息,在重新运行之后并没有写入到文件里,哪里有问题呢
import java.io.*;
import java.util.*;
public class StudentInfo {
Map<Integer,Student> studentMap = new HashMap<>();
Scanner s = new Scanner(System.in);
public void init(){
Student s1 = new Student(1,"zhangsan","男","1990-10-12","zhangsan@123.com");
Student s2 = new Student(2,"lisi","男","1990-12-12","lisi@123.com");
Student s3 = new Student(3,"wangwu","男","1980-11-12","wangwu@123.com");
studentMap.put(s1.getNo(),s1);
studentMap.put(s2.getNo(),s2);
studentMap.put(s3.getNo(),s3);
serializable(studentMap);
}
//查看学生列表
/*
--------------------------------------------------------------------
学生信息列表展示
学号 姓名 性别
------------------------------------
1 zhangsan 男
2 lisi 女
.....
--------------------------------------------------------------------
*/
public void checkAll(){
Map<Integer,Student> studentMap1=deserializable();
Set<Map.Entry<Integer,Student>> entrySet=studentMap1.entrySet();
System.out.println("------------------------学生信息展示页面------------------------------------");
System.out.println("学号\t\t\t姓名\t\t\t性别\t\t\t生日\t\t\t邮箱");
for (Map.Entry<Integer,Student> node:entrySet){
System.out.println(node.getValue());
}
System.out.println("---------------------------------------------------------------------------");
}
/*
保存学生
*/
public void addStudent(){
Map<Integer,Student> studentMap1=deserializable();
System.out.println("请输入学生的信息:");
System.out.print("学号:");
int no=s.nextInt();
System.out.print("姓名:");
String name=s.next();
System.out.print("性别:");
String sex=s.next();
System.out.print("生日:");
String brith=s.next();
System.out.print("邮箱:");
String mailAddress=s.next();
Student newStudent =new Student(no,name,sex,brith,mailAddress);
studentMap1.put(no,newStudent);
serializable(studentMap1);
System.out.println("添加"+name+"成功");
System.out.println("-------------------------");
}
public void serializable(Map<Integer,Student> studentMap){
ObjectOutputStream oos=null;
try {
oos = new ObjectOutputStream(new FileOutputStream("studentsInfo"));
oos.writeObject(studentMap);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (oos!=null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
publicMap<Integer,Student>deserializable() {
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("studentsInfo"));
Map<Integer, Student> studentMap1 = (Map<Integer, Student>) ois.readObject();
return studentMap1;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/*
删除学生
*/
public void deleteStudent(){
Map<Integer,Student> studentMap1=deserializable();
System.out.print("请输入学生的学号:");
int no=s.nextInt();
if (no<0||no>studentMap1.size()){
System.out.println("输入的学号不存在!");
}else {
studentMap1.remove(no);
System.out.println("删除成功");
serializable(studentMap1);
}
}
public void checkStudent(){
Map<Integer,Student> studentMap1=deserializable();
System.out.print("请输入学生的学号:");
int no=s.nextInt();
if (no<0||no>studentMap1.size()){
System.out.println("输入的学号不存在!");
}else {
System.out.println(
"-----------------------------------------------"+"\n"+
"学号:"+studentMap1.get(no).getNo()+"\n"+
"名字:"+studentMap1.get(no).getName()+"\n"+
"性别:"+studentMap1.get(no).getSex()+"\n"+
"生日:"+studentMap1.get(no).getBirth()+"\n"+
"邮箱:"+studentMap1.get(no).getMailAdress()+"\n"+
"-----------------------------------------------"
);
}
}
}
import java.io.Serializable;
import java.util.Objects;
public class Student implements Serializable {
private final static long serialVersionUID=12L;
private int no;
private String name;
private String sex;
private String birth;
private String mailAdress;
public Student(int no, String name, String sex, String birth, String mailAdress) {
this.no = no;
this.name = name;
this.sex = sex;
this.birth = birth;
this.mailAdress = mailAdress;
}
public Student() {
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getMailAdress() {
return mailAdress;
}
public void setMailAdress(String mailAdress) {
this.mailAdress = mailAdress;
}
@Override
public String toString() {
//returnno+"\t\t\t"+name+"\t\t\t"+sex+"\t\t\t"+birth+"\t\t\t"+mailAdress;
return no+"\t\t\t"+String.format("%-10s",name)
+String.format("%12s",sex)+String.format("%20s",birth)
+String.format("%20s",mailAdress);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return no == student.no &&
Objects.equals(name, student.name) &&
Objects.equals(sex, student.sex) &&
Objects.equals(birth, student.birth) &&
Objects.equals(mailAdress, student.mailAdress);
}
@Override
public int hashCode() {
return Objects.hash(no, name, sex, birth, mailAdress);
}
}
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class StudentSystemTest {
public static void main(String[] args) {
StudentInfo si =new StudentInfo();
//si.init();
System.out.println("欢迎使用学生信息管理系统,请认真阅读以下使用说明:");
while (true){
System.out.println("请输入不同的功能编号来选择不同的功能:");
System.out.println("查看学生列表");
System.out.println("保存学生");
System.out.println("删除学生");
System.out.println("查看某个学生详细信息");
System.out.println("退出系统");
Scanner s= new Scanner(System.in);
int n=s.nextInt();
if (n==1){
si.checkAll();
}else if (n==2) {
si.addStudent();
}else if (n==3){
si.deleteStudent();
}else if (n==4){
si.checkStudent();
}else if (n==0){
System.out.println("感谢您的使用~");
return;
}else{
System.out.println("输入的功能编号有误!");
}
}
}
}
页:
[1]