日期:2014-05-20 浏览次数:20828 次
public class classes implements Serializable { private int grade; private String major; private int classnum; private ArrayList stulist; //去掉之后读写无误,此容器用作存放students类 public classes(int grade, String major, int classnum) { this.grade = grade; this.major = major; this.classnum = classnum; this.stulist = new ArrayList(); //去掉之后读写无误 } public void addstudent(students a){ this.stulist.add(a); //添加学生 } public void savaclass() { //写入函数,无错误提示 try { File classFile = new File("D:/TestData/Classes"); FileOutputStream classdata = new FileOutputStream("D:/TestData/Classes/" + classnum + ".dat"); ObjectOutputStream out = new ObjectOutputStream(classdata); out.writeObject(this); classdata.close(); } catch (Exception e) { e.printStackTrace(); } } public classes loadclass(int classnum) { classes a = new classes(); try { FileInputStream classdata = new FileInputStream("D:/TestData/Classes/" + classnum + ".dat"); ObjectInputStream in = new ObjectInputStream(classdata); if (a instanceof classes) { a = (classes) in.readObject(); //但classes不含对象数组或Arraylist时读写无误 //否则会抛出异常local class incompatible } else { System.out.print("Exception"); } classdata.close(); } catch (Exception e) { e.printStackTrace(); } return a; } public void static main(String[] args){ classes a = new classes(2009,"softwareE",5);//产生一个不含学生的班级类 a.addstudent(new students(10,"Tim",5)); a.savaclass(); classes c = new classes();//无参构造函数默认存在,且会对数组初始化,这里没贴上 c = a.loadclass(5); //classes含对象数组时出错,否则正常读出 } class students implements Serializable { private String name; private int stunumber; private int classnum; public students(int stunumber, String name, int classnum) { this.name = name; this.stunumber = stunumber; this.classnum = classnum; } }
import java.util.ArrayList; import java.io.*; public class Classes implements Serializable { private int grade; private String major; public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public int getClassnum() { return classnum; } public void setClassnum(int classnum) { this.classnum = classnum; } public ArrayList<Student> getStulist() { return stulist; } public void setStulist(ArrayList<Student> stulist) { this.stulist = stulist; } private int classnum; private ArrayList<Student> stulist; //去掉之后读写无误,此容器用作存放students类 public Classes(int grade, String major, int classnum) { this.grade = grade; this.major = major; this.classnum = classnum; this.stulist = new ArrayList(); //去掉之后读写无误 } public void addStudent(Student a){ this.stulist.add(a); //添加学生 } public void savaClass() { //写入函数,无错误提示 try { // File classFile = new File("D:/TestData/Classes"); FileOutputStream classdata = new FileOutputStream("D:/TestData/Classes/" + classnum + ".dat"); ObjectOutputStream out = new ObjectOutputStream(classdata); out.writeObject(this); classdata.close(); } catch (Exception e) { e.printStackTrace(); } } public Classes loadClass(int classnum) { Classes a = null; try { FileInputStream classdata = new FileInputStream("D:/TestData/Classes/" + classnum + ".dat"); ObjectInputStream in = new ObjectInputStream(classdata); a = (Classes) in.readObject(); // 一切正常 classdata.close(); } catch (Exception e) { e.printStackTrace(); } return a; } public static void main(String[] args){ Classes a = new Classes(2009,"softwareE",5);//产生一个不含学生的班级类 a.addStudent(new Student(10,"Tim",5)); a.savaClass(); Classes c = a.loadClass(5);//无参构造函数默认存在,且会对数组初始化,这里没贴上 System.out.println(c.getClassnum()); System.out.println(c.getGrade()); System.out.println(c.getMajor()); ArrayList<Student> stus=c.getStulist(); for(Student s:stus){ System.out.println(s); } //classes含对象数组时出错,否则正常读出 } } class Student implements Serializable { private String name; private int stuNumber; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getStuNumber() { return stuNumber; } public void setStuNumber(int stuNumber) { this.stuNumber = stuNumber; } public int getClassNum() { return classNum; } public void setClassNum(int classNum) { this.classNum = classNum; } private int classNum; public Student(int stuNumber, String name, int classNum) { this.name = name; this.stuNumber = stuNumber; this.classNum = classNum; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", stuNumber=" + stuNumber + ", classNum=" + classNum + '}'; } }