public static ScoreMenu[] loadFile() { MainWindow.initDataFile(SCORE_MENU_FILE); DataInputStream input; Course course; Class tempClass; Student student; int[] testTime; long[] id; float[] score; ScoreMenu[] scoreMenu = new ScoreMenu[20]; int count = 0; try { input = new DataInputStream(new FileInputStream(SCORE_MENU_FILE)); while (input.available() > 0) { if (count >= scoreMenu.length) { ScoreMenu[] temp = new ScoreMenu[scoreMenu.length * 2]; for (int i = 0; i < scoreMenu.length; i++) temp[i] = scoreMenu[i]; scoreMenu = temp; } tempClass = new Class(); tempClass.setGrade(input.readInt()); tempClass.setMajor(input.readUTF()); tempClass.setClassNumber(input.readInt()); tempClass.setStudentNumber(0); int num = input.readInt(); for (int i = 0; i < num; i++) { student = new Student(); student.setId(input.readLong()); student.setName(input.readUTF()); tempClass.addStudent(student); } course = new Course(); course.setId(input.readUTF()); course.setName(input.readUTF()); course.setCredit(input.readFloat()); course.setPeriod(input.readInt()); testTime = new int[5]; for (int i = 0; i < 5; i++) testTime[i] = input.readInt(); num = input.readInt(); id = new long[num]; score = new float[num]; for (int i = 0; i < num; i++) id[i] = input.readLong(); for (int i = 0; i < num; i++) score[i] = input.readFloat(); scoreMenu[count] = new ScoreMenu(tempClass, course, testTime, num, id, score); count++; } input.close(); return scoreMenu; } catch (Exception e) { return null; } }
public static ScoreMenu queryFromFile(Class classes, Course courses) { MainWindow.initDataFile(SCORE_MENU_FILE); DataInputStream input; Class tempClass = new Class(); Student student; Course course = new Course(); int[] testTime = new int[5]; int numberOfAttendTestStudent; long idOfStudents[]; float scoreOfTest[]; try { input = new DataInputStream(new FileInputStream(SCORE_MENU_FILE)); while (input.available() > 0) { tempClass.setGrade(input.readInt()); tempClass.setMajor(input.readUTF()); tempClass.setClassNumber(input.readInt()); int studentNumber = input.readInt(); tempClass.setStudentNumber(0); for (int i = 0; i < studentNumber; i++) { student = new Student(); student.setId(input.readLong()); student.setName(input.readUTF()); tempClass.addStudent(student); } course.setId(input.readUTF()); course.setName(input.readUTF()); course.setCredit(input.readFloat()); course.setPeriod(input.readInt()); for (int i = 0; i < 5; i++) testTime[i] = input.readInt(); numberOfAttendTestStudent = input.readInt(); idOfStudents = new long[numberOfAttendTestStudent]; scoreOfTest = new float[numberOfAttendTestStudent]; for (int i = 0; i < numberOfAttendTestStudent; i++) idOfStudents[i] = input.readLong(); for (int i = 0; i < numberOfAttendTestStudent; i++) scoreOfTest[i] = input.readFloat(); if ((classes.equals(tempClass)) && (courses.equals(course))) { input.close(); return new ScoreMenu( tempClass, course, testTime, numberOfAttendTestStudent, idOfStudents, scoreOfTest); } } input.close(); return null; } catch (Exception e) { return null; } }
public boolean saveToFile() { MainWindow.initDataFile(SCORE_MENU_FILE); DataInputStream input; DataOutputStream output; Class tempClass = new Class(); Student student = new Student(); Course course = new Course(); int[] testTime = new int[5]; int numberOfAttendTestStudent; long idOfStudents[]; float scoreOfTest[]; try { input = new DataInputStream(new FileInputStream(SCORE_MENU_FILE)); while (input.available() > 0) { tempClass.setGrade(input.readInt()); tempClass.setMajor(input.readUTF()); tempClass.setClassNumber(input.readInt()); int studentNumber = input.readInt(); tempClass.setStudentNumber(0); for (int i = 0; i < studentNumber; i++) { student.setId(input.readLong()); student.setName(input.readUTF()); } course.setId(input.readUTF()); course.setName(input.readUTF()); course.setCredit(input.readFloat()); course.setPeriod(input.readInt()); for (int i = 0; i < 5; i++) testTime[i] = input.readInt(); numberOfAttendTestStudent = input.readInt(); idOfStudents = new long[numberOfAttendTestStudent]; scoreOfTest = new float[numberOfAttendTestStudent]; for (int i = 0; i < numberOfAttendTestStudent; i++) idOfStudents[i] = input.readLong(); for (int i = 0; i < numberOfAttendTestStudent; i++) scoreOfTest[i] = input.readFloat(); if ((this.classOfScoreMenu.equals(tempClass)) && (this.course.equals(course))) { input.close(); return false; } } input.close(); output = new DataOutputStream(new FileOutputStream(SCORE_MENU_FILE, true)); output.writeInt(this.classOfScoreMenu.getGrade()); output.writeUTF(this.classOfScoreMenu.getMajor()); output.writeInt(this.classOfScoreMenu.getClassNumber()); int num = this.classOfScoreMenu.getStudentNumber(); output.writeInt(num); Student[] students = this.classOfScoreMenu.getStudents(); for (int i = 0; i < num; i++) { output.writeLong(students[i].getId()); output.writeUTF(students[i].getName()); } output.writeUTF(this.course.getId()); output.writeUTF(this.course.getName()); output.writeFloat(this.course.getCredit()); output.writeInt(this.course.getPeriod()); for (int i = 0; i < 5; i++) output.writeInt(this.testTime[i]); output.writeInt(this.numberOfAttendTestStudent); for (int i = 0; i < this.numberOfAttendTestStudent; i++) output.writeLong(this.idOfStudents[i]); for (int i = 0; i < this.numberOfAttendTestStudent; i++) output.writeFloat(this.scoreOfTest[i]); output.close(); return true; } catch (Exception e) { return false; } }