当前位置: 代码迷 >> J2SE >> 关于ObjectInputStream读写,该怎么解决
  详细解决方案

关于ObjectInputStream读写,该怎么解决

热度:313   发布时间:2016-04-24 18:11:40.0
关于ObjectInputStream读写
想把自定义类的实例写进对象文件,打算用ObjectInputStream
一般情况下都能读写,但,问题来了,当我自定义类的数据域里面包含了对象数组,或ArrayList类之类的数组对象之后,读出会出错。这里贴出一些代码(思路简单,但可能被我写得繁琐):
Java code
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;    }}


我想将整个班级的信息写到一个对象文件了,因此必须借助数组,请问为什么当classes类数据域存在数组时读写失败呢?如何解决?有其他解决办法吗?在线等,谢谢!

------解决方案--------------------
你写的代码严重不符合Java命名规范,改了相当一部分,现在能跑起来了,你用ObjectStream的代码有些根本是多此一举,还有一个instanceof太蛋疼了,贴出代码你对比着看着吧
Java code
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 +                '}';    }}