package StudentIOTest02; import java.io.*; import java.util.*; public class Student implements java.io.Serializable { @Serial private static final long serialVersionUID = -6284145330510018985L; private int no; private String name; private boolean sex; private Birth date;//不參與序列化 private Serializable serializable; private DeSerializable deSerializable; private transient Liststudents; //list使用transient關(guān)鍵字 是為了給toString一個標(biāo)記 運用三目運算符來決定toString輸出形式 //(當(dāng)然可以用兩個toString方法) public Student() { // 默認(rèn)初始化 this.serializable = new Serializable(); this.deSerializable = new DeSerializable(); this.students = new ArrayList (); } public Student(int no, String name, boolean sex, Birth date) { this.no = no; this.name = name; this.sex = sex; this.date = date; } // 由于進(jìn)入系統(tǒng)需要初始化已有學(xué)生 public void loading() throws IOException { this.students.add(new Student(1,"zhangsan",true,new Birth(2004,5,2))); this.students.add(new Student(2,"lisi",true,new Birth(1981,7,14))); this.serializable.ouputStrean(this.students); } // [1]查看學(xué)生列表 public void checkList() throws IOException, ClassNotFoundException { this.deSerializable.inputStream(); } // [2]增加學(xué)生 public void addStudent() throws IOException { while(true) { System.out.println("請輸入學(xué)生的學(xué)號"); Scanner scanner = new Scanner(System.in); int num =scanner.nextInt(); //增加需要在末尾加 獲取學(xué)號大值 加入學(xué)生 if(num == this.students.size()+1) { System.out.println("請依次輸入學(xué)生的姓名、性別、出生年月日"); this.students.add(new Student(num,scanner.next(),scanner.nextBoolean(),new Birth(scanner.nextInt(),scanner.nextInt(),scanner.nextInt()))); this.serializable.ouputStrean(this.students); System.out.println("添加成功"); this.serializable.ouputStrean(this.students); Collections.sort(this.students, new Comparator () { @Override public int compare(Student o1, Student o2) { return o1.no - o2.no; } }); break; }else System.out.println("請重新輸入"); } } // [3]刪除學(xué)生 public void deleteStudent( ) throws IOException { while(true) { Scanner scanner = new Scanner(System.in); int n1 = scanner.nextInt(); if(n1 >0 && n1< this.students.size()) { //移除元素是需要-1 因為是下標(biāo) this.students.remove(n1 - 1); this.serializable.ouputStrean(this.students); System.out.println("刪除成功"); this.serializable.ouputStrean(this.students); //進(jìn)行排序 Collections.sort(this.students, new Comparator () { @Override public int compare(Student o1, Student o2) { return o1.no - o2.no; } }); break; }else System.out.println("您輸入的學(xué)號不存在,重新輸入"); } } //[4]查看某個學(xué)生詳細(xì)信息 public void readAll() { while(true) { System.out.println("輸入您要查看學(xué)生的學(xué)號"); Scanner scanner = new Scanner(System.in); int no = scanner.nextInt(); if (no == 0 || no >this.students.size()) { System.out.println("不存在該學(xué)生"); }else if(! (this.students.get(no-1) instanceof Student)) { System.out.println("不存在該學(xué)生,請重新輸入"); }else { System.out.println(this.students.get(no-1).toString()); break; } } } // 查找某位學(xué)生是否存在 public void searchStudent() { while(true) { System.out.println("輸入您要查找學(xué)生的學(xué)號"); Scanner scanner = new Scanner(System.in); int no = scanner.nextInt(); if (no == 0 || no >this.students.size()) { System.out.println("不存在該學(xué)生"); }else if(! (this.students.get(no-1) instanceof Student)) { System.out.println("不存在該學(xué)生"); }else { System.out.println("找到了該學(xué)生?。?); System.out.println(this.students.get(no-1).toString()); break; } } } // 6 修改某位同學(xué)的信息 public void correct() throws IOException { while(true) { System.out.println("輸入您所修改學(xué)生的學(xué)號"); Scanner scanner = new Scanner(System.in); int no = scanner.nextInt(); if (no == 0 || no >this.students.size()) { System.out.println("不存在該學(xué)生"); } else if (!(this.students.get(no - 1) instanceof Student)) { System.out.println("不存在該學(xué)生"); } else { Scanner scanner1 = new Scanner(System.in); System.out.println("請依次輸入該學(xué)生的學(xué)號、姓名、性別(True或False[True==男])、出生年、月、日"); Student student = new Student(scanner1.nextInt(),scanner.next(),scanner.nextBoolean(), new Birth(scanner1.nextInt(),scanner1.nextInt(),scanner1.nextInt())); this.students.remove(no);//移除該學(xué)生 this.students.add(student);//加入新的信息 this.serializable.ouputStrean(this.students); // 按照升序排序 Collections.sort(this.students, new Comparator () { @Override public int compare(Student o1, Student o2) { return o1.no - o2.no; } }); break; } } } 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 boolean isSex() { return sex; } public void setSex(boolean sex) { this.sex = sex; } public Birth getDate() { return date; } public void setDate(Birth date) { this.date = date; } public Serializable getSerializable() { return serializable; } public void setSerializable(Serializable serializable) { this.serializable = serializable; } public DeSerializable getDeSerializable() { return deSerializable; } public void setDeSerializable(DeSerializable deSerializable) { this.deSerializable = deSerializable; } public List getStudents() { return students; } public void setStudents(List students) { this.students = students; } @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; } @Override public String toString() { return (this.students == null)?("學(xué)生學(xué)號"+this.getNo()+"\n"+ "名字"+this.getName()+"\n"+ "性別"+(this.isSex()?"男":"女")+"\n"+ "生日"+this.date.getYear()+"-"+this.date.getMonth()+""+this.date.getDay()):"學(xué)生學(xué)號"+this.getNo()+"\t"+ "名字"+this.getName()+"\t"+ "性別"+(this.isSex()?"男":"女")+"\t" ; } @Override public int hashCode() { return Objects.hash(no); } } //學(xué)生生日 class Birth implements java.io.Serializable { @Serial private static final long serialVersionUID = 4769685585810521642L; private int year; private int month; private int day; public Birth() { } public Birth(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } } //序列化 class Serializable { public void ouputStrean(List students) throws IOException { ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("C:\\Users\\33769\\Desktop\\JavaTestIOFile\\Test02")); outputStream.writeObject(students); // 刷新 outputStream.flush(); // 關(guān)閉 outputStream.close(); } } //反序列化 class DeSerializable { public void inputStream() throws IOException, ClassNotFoundException { ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("C:\\Users\\33769\\Desktop\\JavaTestIOFile\\Test02")); List students1 = (List ) inputStream.readObject(); for (Student student: students1 ) { System.out.println(student.toString()); } // 關(guān)閉 inputStream.close(); } }
Test測試實現(xiàn)代碼:
成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)由有經(jīng)驗的網(wǎng)站設(shè)計師、開發(fā)人員和項目經(jīng)理組成的專業(yè)建站團隊,負(fù)責(zé)網(wǎng)站視覺設(shè)計、用戶體驗優(yōu)化、交互設(shè)計和前端開發(fā)等方面的工作,以確保網(wǎng)站外觀精美、網(wǎng)站設(shè)計、做網(wǎng)站易于使用并且具有良好的響應(yīng)性。package StudentIOTest02; import java.io.IOException; import java.util.Scanner; public class Test { public static void main(String[] args) { // 初始化系統(tǒng) Student student = new Student(); try { student.loading();//加載學(xué)生 } catch (IOException e) { throw new RuntimeException(e); } Scanner scanner = new Scanner(System.in); A: while(true) { System.out.println("歡迎使用學(xué)生信息管理系統(tǒng),請認(rèn)真閱讀以下使用說明:\n" + "請輸入不同的功能編號來選擇不同的功能:\n" + "[1]查看學(xué)生列表\n" + "[2]增加學(xué)生 \n" + "[3]刪除學(xué)生\n" + "[4]查看某個學(xué)生詳細(xì)信息\n"+ "[5]查找某位學(xué)生\n"+ "[6]修改某位同學(xué)的信息"); int no = scanner.nextInt(); if(no<0 || no >6) { System.out.println("請重新輸入"); }else if(no == 1) { try { student.checkList(); continue A; } catch (IOException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } }else if(no == 2) { try { student.addStudent(); continue A; } catch (IOException e) { throw new RuntimeException(e); } } else if (no == 3) { try { student.deleteStudent(); continue A; } catch (IOException e) { throw new RuntimeException(e); } }else if(no == 4) { student.readAll(); continue A; }else if(no == 5) { student.searchStudent(); continue A; }else if (no == 6) { try { student.correct(); continue A; } catch (IOException e) { throw new RuntimeException(e); } } } } }
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧