读取文件内容出现java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: elective.system.Elective错误

1.在读取文件中的内容时出现标题错误,Elective类已经继承Serializable接口

```package elective.system;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;

/*
选修课程类
*/
public class Elective implements Serializable {
private int class_num; //选修课程编号
private String class_nature; //课程类别
private String class_name; //选修课名称
private String class_teacher; //课程教师
private int class_credit; //课程学分
private String class_time; //课程起止周
private String class_daily_time; //课程日常上课时间
private String class_school; //课程承办的二级学院
private int class_person_num; //课程人数
private String class_note; //课程备注
//无参构造方法
public Elective() {
}

//有参构造方法


public Elective(int class_num, String class_nature, String class_name, String class_teacher, int class_credit, String class_time, String class_daily_time, String class_school, int class_person_num, String class_note) {
    this.class_num = class_num;
    this.class_nature = class_nature;
    this.class_name = class_name;
    this.class_teacher = class_teacher;
    this.class_credit = class_credit;
    this.class_time = class_time;
    this.class_daily_time = class_daily_time;
    this.class_school = class_school;
    this.class_person_num = class_person_num;
    this.class_note = class_note;
}

//getter和setter方法


public int getClass_person_num() {
    return class_person_num;
}

public void setClass_person_num(int class_person_num) {
    this.class_person_num = class_person_num;
}

public int getClass_num() {
    return class_num;
}

public void setClass_num(int class_num) {
    this.class_num = class_num;
}

public String getClass_name() {
    return class_name;
}

public void setClass_name(String class_name) {
    this.class_name = class_name;
}

public String getClass_teacher() {
    return class_teacher;
}

public void setClass_teacher(String class_teacher) {
    this.class_teacher = class_teacher;
}

public int getClass_credit() {
    return class_credit;
}

public void setClass_credit(int class_credit) {
    this.class_credit = class_credit;
}

public String getClass_time() {
    return class_time;
}

public void setClass_time(String class_time) {
    this.class_time = class_time;
}

public String getClass_daily_time() {
    return class_daily_time;
}

public void setClass_daily_time(String class_daily_time) {
    this.class_daily_time = class_daily_time;
}

public String getClass_nature() {
    return class_nature;
}

public void setClass_nature(String class_nature) {
    this.class_nature = class_nature;
}

public String getClass_note() {
    return class_note;
}

public void setClass_note(String class_note) {
    this.class_note = class_note;
}

public String getClass_school() {
    return class_school;
}

public void setClass_school(String class_school) {
    this.class_school = class_school;
}

//增加选修课程
public boolean add_class(Elective elective){
    boolean flag = false;
    File file = new File("D:\\IdeaProjects\\basic-code\\01-code\\选修课程表\\class.txt");
    OperateFile operateFile = new OperateFile();
    try{
        operateFile.save(elective,file);
        flag = true;
    }catch (Exception e){
        e.printStackTrace();
    }
    return flag;
}

//查看已有选修课程
public ArrayList<Elective> look_class(){
    ArrayList<Elective> list = new ArrayList<>();
    File file = new File("D:\\IdeaProjects\\basic-code\\01-code\\选修课程表\\class.txt");
    OperateFile operateFile = new OperateFile();
    try{
        list = operateFile.load(file);
    }catch (Exception e){
        e.printStackTrace();
    }
    return list;
}

@Override
public String toString() {
    return "课程编号:" + class_num +
            ", 课程类别:" + class_nature +
            ", 课程名称:" + class_name +
            ", 授课教师:" + class_teacher +
            ", 学分:" + class_credit +
            ", 课程起止周:" + class_time +
            ", 课程上课时间:" + class_daily_time +
            ", 课程承办学院:" + class_school+
            ", 课程备注:" + class_note;
}

}

这是文件保存和读取类

package elective.system;

import java.io.*;
import java.util.ArrayList;

/*
文件操作类
 */
public class OperateFile implements Serializable{

    public boolean save(Object e,File file) throws Exception{                         // 保存对象
        boolean flag = false ;                                                // 定义操作标志位
        FileOutputStream fos = new FileOutputStream(file,true);       //采用追加方式写入数据
        if(file.length()<1){
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(e);
            flag = true;
            oos.close();
        }else{
            MyObjectOutputStream mos = new MyObjectOutputStream(fos);
            mos.writeObject(e);
            flag = true;
            mos.close();
        }
        return flag ;
    }
    public ArrayList load(File file) throws Exception{                          // 读取对象
        ArrayList<Object> list = new ArrayList();
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        while(fis.available()>0){
            Object p =  ois.readObject();
            list.add(p);
        }
        ois.close();
        return list;
    }
}
class MyObjectOutputStream  extends ObjectOutputStream{

    public MyObjectOutputStream(OutputStream out) throws IOException {
        super(out);
    }

    public void writeStreamHeader() throws IOException{
        return;
    }
}

这是操作:
Elective elective = new Elective();
ArrayList list = elective.look_class();
for(Elective et : list){
System.out.println(et.toString());
}

请问各位大佬这是为什么呢?

1个回答

在实体类中,加一个无参的构造函数试试!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐