学生管理系统(文件流)
2017-09-11 13:14
176 查看
import java.io.Serializable; public class Student implements Comparable<Student>,Serializable{ private int sno; private String name; private int age; private char sex; private String address; @Override public boolean equals(Object obj) { } @Override public String toString() { return "Student [sno=" + sno + ", name=" + name + ", age=" + age + ", sex=" + sex + ", address=" + address + "]"; } public Student(int sno, String name, int age, char sex, String address) { } @Override public int compareTo(Student o) { if (sno>o.getSno()) return 1; else return -2; } }
import java.io.EOFException; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Scanner; /** * 这是一个简易的学生管理系统,通过io文件流以及对象输入输出流的运用 * 实现数据的文件永久储存。编写代码中遇到过以下问题,大部分已解决: * 1.下次使用时,储存文件数据导入失败(ObjectOutputStream(false) 声明时自动将文件清空) * 2.对象输入输出流在 static代码块中不能正常运行??; * 3.如果使用ObjectOutputStream(true)会导致每次运行追加输入(header),从而读取失败 * 解决方法: * 1.定义MyObjectOutputStream(true),实现判断文件是否为空,若不是则不追加header * 2.使用ObjectOutputStream(false)不追加输入。 两种思路: * 1.直接一次将学生数组写入文件(Arraylist)、普通数组会报错(实现接口)?? * 2.多次将学生一个个写入文件 * **3.每次读对象之前,read跳过四个字节** * */ public class StudentManagementSystem { public static FileOutputStream os; public static ObjectOutputStream oos; public static FileInputStream is; public static ObjectInputStream ois; /*static { try { } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ public static int size=5; public static int times=0;//登陆次数 static Student[] studentList=new Student[size] ; public static int length=0;//数组下标 public static void addStudent(Student stu) throws IOException { if(length==0) { studentList[length++]=stu; } else { if(length>=size) {//数组扩容 size=2*size; Student[] studentList2=new Student[size]; for(int i=0;i<length;i++) { studentList2[i]=studentList[i]; } studentList=studentList2; } for(int i=0;i<length;i++) { if(studentList[i].getSno()==stu.getSno()) { System.out.println("学号不能重复,增加失败"); return; } } studentList[length++]=stu; } } public static void changeStudent(int sno,int age,String address) { } //查询 public static void seeStudent(int sno) { } public static void deleteStudent(int sno) { } public static void showStudent() { } public static void sortStudent() { } public static void main(String []args) throws IOException, ClassNotFoundException { is=new FileInputStream("c:\\jdk\\c.txt"); ois=new ObjectInputStream(is); //**oos若在此声明,会导致上一次写入文件的数据清空 /* os=new FileOutputStream("c:\\jdk\\c.txt"); oos=new ObjectOutputStream(os);*/ //想直接输入数组,一次搞定,但数据要是实现Serializable接口(不知道格式) /* Student s3=new Student(3, "jack", 12, '男', "杭州"); Student s2=new Student(2, "jack", 12, '男', "杭州"); Student s5=new Student(5, "jack", 12, '男', "杭州"); Student s4=new Student(4, "jack", 12, '男', "杭州"); addStudent(s1); addStudent(s2); oos.writeObject(studentList); try { studentList=(Student[]) ois.readObject(); } catch(EOFException e) { System.out.println("还未添加任何学生"); }*/ //以下方法由于多次追加写入文件(开关多次会使下次输入带header)导致 //每次重新打开oos 会输入一个header() //下次读出错(invalid type code: AC)静态代码块实现oos /* while(true){ try { s=(Student)ois.readObject(); System.out.println(s); studentList[length++]=s; } catch (EOFException e) { // TODO Auto-generated catch block break; } } */ Student s; while(true){ try { s=(Student)ois.readObject(); System.out.println(s); studentList[length++]=s; } catch (EOFException e) { // TODO Auto-generated catch block System.out.println("导入结束"); ois.close(); break; } } Scanner sc=new Scanner(System.in); while(true) { log: while(true) { System.out.println("1--登陆——————————2--注册"); int a=sc.nextInt(); switch (a) { case 1: if(LogIn.login.size()==0) System.out.println("还未有账户,请先注册"); else { System.out.println("请输入账号:"); String id=sc.next(); System.out.println("请输入密码"); String password=sc.next(); if(LogIn.hasLog(id, password)) { System.out.println("登陆成功"); times++; break log; }else System.out.println("账户不对"); } break; case 2: while(true) { System.out.println("请输入账号:"); String id=sc.next(); System.out.println("请输入密码"); String password=sc.next(); if(LogIn.hasId(id)) { System.out.println("该账号已存在"); } else { new LogIn(id,password); System.out.println("注册成功"); break ; } } break; default: System.out.println("输入有误"); break; } } in: while(true) { System.out.println("--------欢迎进入学生管理系统--------"); System.out.println("1.增加学生 2.删除学生 3.修改学生 4.查询学生 5.输出名单 6.排序输出 7.返回 8.退出 "); System.out.println("请输入序号:"); int a=sc.nextInt(); switch (a) { case 1: System.out.println("请输入学号:"); int sno=sc.nextInt(); System.out.println("请输入姓名:"); String name=sc.next(); System.out.println("请输入性别:"); char sex=sc.next().charAt(0); System.out.println("请输入年龄:"); int age=sc.nextInt(); System.out.println("请输入地址:"); String address=sc.next(); addStudent(new Student(sno, name, age, sex, address)); break; case 2: System.out.println("请输入学号:"); int sno2=sc.nextInt(); deleteStudent(sno2); break; case 3: System.out.println("请输入学号:"); int sno3=sc.nextInt(); System.out.println("请输入年龄:"); int age3=sc.nextInt(); System.out.println("请输入地址:"); String address3=sc.next(); changeStudent(sno3, age3, address3); break; case 4: System.out.println("请输入学号:"); int sno4=sc.nextInt(); seeStudent(sno4); break; case 5: showStudent(); break; case 8: os=new FileOutputStream("c:\\jdk\\c.txt");//*** oos=new ObjectOutputStream(os); System.out.println("使用次数:"+times); System.out.println("欢迎下次使用"); for(int i=0;i<length;i++) { oos.writeObject(studentList[i]); } oos.close(); return; case 7: break in; case 6: sortStudent(); showStudent(); break; default: System.out.println("输入有误"); break; } } } }
相关文章推荐
- 学生信息管理系统4
- Java实现学生管理系统
- 学生信息管理系统(在LINUX下用GCC编译需要更改conio.h为curses.h并修改相关代码)
- javascript写的学生管理系统,简单的表格操作——小小钟
- 《C语言编写 学生成绩管理系统》
- Java 学生成绩管理系统
- 【原创视频教程】学生信息管理系统2--主窗体
- 【学生信息管理系统 总结】
- 学生管理系统
- 学生信息管理系统--登录调试(错误‘91’)
- 《C语言编写 学生成绩管理系统》
- 共同改学生管理系统的错
- 学生信息管理系统4——数据库的发展
- 【原创视频教程】学生信息管理系统4--成绩添加
- 分享这次高级JAVA的大作业..学生成绩管理系统
- java学习--学生成绩管理系统
- 简易学生管理系统。。。。
- 【学生信息管理系统】与后端系统接口
- 用C语言实现的学生管理系统