为了账号安全,请及时绑定邮箱和手机立即绑定

通过对象流完成小型学生管理系统的设计与实现

标签:
Android

1.设置序列化的对象实体

package demo02;

import java.io.Serializable;

public class Student implements Serializable{

 /**

 * 

 */

private static final long serialVersionUID = 1L;

private int id;

 private String name;

 private int age;

public Student() {

super();

// TODO Auto-generated constructor stub

}

public Student(int id, String name, int age) {

super();

this.id = id;

this.name = name;

this.age = age;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public String toString() {

return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";

}

 

}

2.test.java文件实现对学生的具体操作

package demo02;

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.List;

import java.util.Scanner;

public class Test {

/**

 * @param args

 */

public static void main(String[] args) {

while(true){

System.out.println("=====================欢迎光临千峰学生管理系统====================");

System.out.println("1:添加学生信息!");

System.out.println("2:删除学生信息!");

System.out.println("3:查看学生信息!");

System.out.println("4:修改学生信息!");

System.out.println("5:退出登录!");

Scanner sc=new Scanner(System.in);

System.out.println("请选择您的操作:");

int choose=sc.nextInt();

switch (choose) {

    case 1:

    addStudent();

break;

    case 2:

    delStudent();

break;

    case 3:

    findStudent();

break;

    case 4:

    modifyStudent();

break;

    case 5:

     System.out.println("退出成功!");

    break;

}

System.out.println("请按任意键回到首页");

int id=sc.nextInt();

}

}

private static void modifyStudent() {

File file=new File("F:\\学习资料\\student.txt");

Scanner sc=new Scanner(System.in);

System.out.println("请输入修改学生的学号");

int id=sc.nextInt();

System.out.println("请输入修改学生的姓名");

String name=sc.next();

System.out.println("请输入修改学生的年龄");

int age=sc.nextInt();

Student stu=new Student(id,name,age);

    List<Student>list=new ArrayList<Student>();

try {

ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));

list=(List<Student>) ois.readObject();

List<Student>list2=new ArrayList<Student>();

for (Student student : list) {

if(id!=student.getId()){

list2.add(stu);

}

}

list2.add(stu);

ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));

oos.writeObject(list2);

System.out.println("修改成功!");

oos.close();

ois.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

    

}

private static void findStudent() {

File file=new File("F:\\学习资料\\student.txt");

try {

ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));

List<Student>list=new ArrayList<Student>();

list=(List<Student>) ois.readObject();

for (Student student : list) {

  System.out.println(student.toString());

}

ois.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

           

}

private static void delStudent() {

File file=new File("F:\\学习资料\\student.txt");

Scanner sc=new Scanner(System.in);

System.out.println("请输入删除学生的学号");

int id=sc.nextInt();

try {

ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));

List<Student>list=new ArrayList<Student>();

List<Student>list2=new ArrayList<Student>();

list=(List<Student>) ois.readObject();

for (Student student : list) {

if(id!=student.getId()){

list2.add(student);

}

}

ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));

oos.writeObject(list2);

System.out.println("删除成功!");

oos.close();

ois.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

private static void addStudent() {

        

File file=new File("F:\\学习资料\\student.txt");

Scanner sc=new Scanner(System.in);

System.out.println("请输入添加学生的学号");

int id=sc.nextInt();

System.out.println("请输入添加学生的姓名");

String name=sc.next();

System.out.println("请输入添加学生的年龄");

int age=sc.nextInt();

Student stu=new Student(id,name,age);

    List<Student>list=new ArrayList<Student>();

if(file.exists()){

try {

ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));

    list=(List<Student>)ois.readObject();

    ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));

    list.add(stu);

    oos.writeObject(list);

    System.out.println("学生添加成功!");

oos.close();

ois.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}else{

  try {

            list.add(stu);

ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));

oos.writeObject(list);//添加学生对象

System.out.println("添加成功!");

oos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

  

}

}

}

原文链接:http://www.apkbus.com/blog-813041-60938.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消