sky5412 2020-05-29 20:22 采纳率: 0%
浏览 461

读取文件内容出现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条回答 默认 最新

  • sanshizhang 2020-06-01 18:06
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀