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一個(gè)標(biāo)記 運(yùn)用三目運(yùn)算符來決定toString輸出形式
//(當(dāng)然可以用兩個(gè)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("請(qǐng)輸入學(xué)生的學(xué)號(hào)");
Scanner scanner = new Scanner(System.in);
int num =scanner.nextInt();
//增加需要在末尾加 獲取學(xué)號(hào)大值 加入學(xué)生
if(num == this.students.size()+1)
{
System.out.println("請(qǐng)依次輸入學(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("請(qǐng)重新輸入");
}
}
// [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 因?yàn)槭窍聵?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é)號(hào)不存在,重新輸入");
}
}
//[4]查看某個(gè)學(xué)生詳細(xì)信息
public void readAll()
{
while(true)
{
System.out.println("輸入您要查看學(xué)生的學(xué)號(hào)");
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é)生,請(qǐng)重新輸入");
}else
{
System.out.println(this.students.get(no-1).toString());
break;
}
}
}
// 查找某位學(xué)生是否存在
public void searchStudent()
{
while(true)
{
System.out.println("輸入您要查找學(xué)生的學(xué)號(hào)");
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é)號(hào)");
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("請(qǐng)依次輸入該學(xué)生的學(xué)號(hào)、姓名、性別(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 ListgetStudents() {
return students;
}
public void setStudents(Liststudents) {
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é)號(hào)"+this.getNo()+"\n"+
"名字"+this.getName()+"\n"+
"性別"+(this.isSex()?"男":"女")+"\n"+
"生日"+this.date.getYear()+"-"+this.date.getMonth()+""+this.date.getDay()):"學(xué)生學(xué)號(hào)"+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(Liststudents) 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"));
Liststudents1 = (List) inputStream.readObject();
for (Student student: students1
) {
System.out.println(student.toString());
}
// 關(guān)閉
inputStream.close();
}
} Test測試實(shí)現(xiàn)代碼:

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),請(qǐng)認(rèn)真閱讀以下使用說明:\n" +
"請(qǐng)輸入不同的功能編號(hào)來選擇不同的功能:\n" +
"[1]查看學(xué)生列表\n" +
"[2]增加學(xué)生 \n" +
"[3]刪除學(xué)生\n" +
"[4]查看某個(gè)學(xué)生詳細(xì)信息\n"+
"[5]查找某位學(xué)生\n"+
"[6]修改某位同學(xué)的信息");
int no = scanner.nextInt();
if(no<0 || no >6)
{
System.out.println("請(qǐng)重新輸入");
}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海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
文章名稱:序列化反序列化實(shí)現(xiàn)學(xué)生管理系統(tǒng)(小白級(jí)別的很麻煩)-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://www.chinadenli.net/article4/djoiie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、商城網(wǎng)站、網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)