java对象序列化技术的运用——学生管理系统

本文介绍了一个使用Java对象序列化技术实现的学生管理系统,该系统能在控制台进行数据交互,并能将数据持久化存储,确保断线后数据不丢失。系统包括学生类、学生管理类和client类。学生类包含基本属性和方法;学生管理类封装了序列化和反序列化操作,以动态数组管理学生对象,并实现了增删改查功能;client类则负责交互界面,采用switch语句处理用户操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目要求

  • 实现一个学生管理系统,在控制台进行输入输出
  • 将数据存入本地文档,实现断线后数据不丢失功能
  • 管理系统具备增删改查排序等功能

思路

    这个项目的难点在于对象序列化的技术。对象序列化技术就是将数据放入本地文档,可以让数据不仅仅存在于内存之中。在这个项目中,可提前对序列化和反序列化方法进行封装,提高代码复用率。本项目总共三个类。学生类,学生管理类与client类。

学生类

	public Student(int sno, String sname, String sex, int age) {
		super();
		this.sno = sno;
		this.sname = sname;
		this.sex = sex;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [sno=" + sno + ", sname=" + sname + ", sex=" + sex + ", age=" + age + "]";
	}
	public int getSno() {
		return sno;
	}
	public void setSno(int sno) {
		this.sno = sno;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}

    这个类是对学生的定义,包含set、get方法和构造器

学生管理类

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;

public class StudentManage {

	
	ArrayList<Student> list=new ArrayList<>();
	Scanner sc=new Scanner(System.in);
	Scanner sc2=new Scanner(System.in);
	
	public void output() throws FileNotFoundException, IOException{
		ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("src/students.txt"));
		oos.writeObject(list);
		oos.close();
	}
	public void input() throws FileNotFoundException, IOException, ClassNotFoundException{
		File file=new File("src/students.txt");
		if(!file.exists()){
			file.createNewFile();
		}
		if(file.length()==0){
			output();
		}
		ObjectInputStream ois=new ObjectInputStream(new FileInputStream("src/students.txt"));
		list=(ArrayList<Student>) ois.readObject();
		ois.close();
	}
	public void add() throws FileNotFoundException, ClassNotFoundException, IOException{
		input();
		System.out.println("请按如下格式输入:sno/sname/sex/age");
		
		String msg=sc.nextLine();
		String[] info=msg.split("/");
		list.add(new Student(Integer.parseInt(info[0]),info[1],info[2],Integer.parseInt(info[3])));
		output();
	}
	public void change() throws FileNotFoundException, ClassNotFoundException, IOException{
		input();
		System.out.println("请输入你想修改学生的学号");
		int sno=sc.nextInt();
		for(Student s:list){
			if(s.getSno()==sno){
				System.out.println("学生的信息修改为:sno/sname/sex/age");
				String msg=sc2.nextLine();
				String[] info=msg.split("/");
				s.setSno(Integer.parseInt(info[0]));
				s.setSname(info[1]);
				s.setSex(info[2]);
				s.setAge(Integer.parseInt(info[3]));
			}else{
				System.out.println("学生不存在");
			}
		}
		output();
	}
	public void showAll() throws FileNotFoundException, ClassNotFoundException, IOException{
		input();
		sort1();
		for(Student s:list){
			System.out.println(s.toString());
		}
	}
	public void delete() throws FileNotFoundException, ClassNotFoundException, IOException{
		input();
		System.out.println("请输入要删除学生的编号");
		int sno=sc.nextInt();

		Iterator<Student> it= list.iterator();
		while(it.hasNext()){
			if(sno==it.next().getSno()){
				it.remove();
			}
		}
		output();
	}
	public void sort1() throws FileNotFoundException, IOException{
		Collections.sort(list,(a,b)->a.getAge()-b.getAge());
		output();
	}
}

    这个类是对本项目功能的实现。首先定义了一个动态数组用于临时存放学生的相关数据。本项目需要将学生对象放入数组中,再对数组进行操作,因为反序列化不好取出多行数据。在这个项目中,我首先对系列化与反序列化方法进行了封装。对文件进行反序列化前,需先进行判断文件是否存在或是否为空。后面的增删改查操作与普通操作系统的增删改查操作一样,但在操作前需将提前准备好的数组用于接收反序列化的内容。除查这个操作外,其余操作结束均要进行序列化。

client类

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class Client {
	Scanner sc=new Scanner(System.in);
	public void start() throws FileNotFoundException, ClassNotFoundException, IOException{
		System.out.println("***1.添加学生***2.修改学生***3.查询全体学生***4.删除学生***5.退出系统***");
		System.out.println("请选择操作:");
		StudentManage sm=new StudentManage();
		int i=sc.nextInt();
		switch(i){
		case 1:
			sm.add();
			start();
			break;
		case 2:
			sm.change();
			start();
			break;
		case 3:
			sm.showAll();
			start();
			break;
		case 4:
			sm.delete();
			start();
			break;
		case 5:
			System.out.println("Thanks for using!!!");
			return;
			default:
				System.out.println("输入错误,请重新输入");
				start();
		}
	}

	public static void main(String[] args) throws FileNotFoundException, ClassNotFoundException, IOException {
		Client c=new Client();
		c.start();
	}
}

    这个类主要是对界面的实现,可以让用户在操作台上进行操作。主要是switch语法的应用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值