Java--->编程题之序列化

Java--->编程题之序列化

题目

  • 定义一个可序列化的对象Student类,这个类实现了Serializable接口,类中的成员变量id,name,age,department都可以被序列化,方法不能序列化。通过对象输出流的writeObject()方法将Student对象保存到文件data.ser中, 然后通过对象输入流的readObject()方法从文件data.ser中读出保存下来的Student对象,然后将Student对象的id和name输出到控制台

代码

class  student implements Serializable {
    private static final long SerializableUID = 10000000000l;
    private int stuno;
    private String name;
    private int score;


    public student(int stuno, String name, int score) {
        this.stuno = stuno;
        this.name = name;
        this.score = score;
    }

    public int getStuno() {
        return stuno;
    }

    public void setStuno(int stuno) {
        this.stuno = stuno;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        student student = (student) o;
        return stuno == student.stuno;
    }

    @Override
    public int hashCode() {
        return Objects.hash(stuno);
    }

    @Override
    public String toString() {
        return "student{" +
                "stuno=" + stuno +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
    public void setScore(int score) {
        this.score = score;
    }
}

public class Test04 {
    public static void main(String[] args) {
        ObjectOutputStream os = null;
        ObjectInputStream is = null;

        try {
            os = new ObjectOutputStream(new FileOutputStream("stu.txt"));
            is = new ObjectInputStream(new FileInputStream("stu.txt"));
            student stu1 = new student(194041201, "张三", 90);
            student stu2 = new student(194041202, "李四", 30);
            student stu3 = new student(194041203, "王五", 70);
            student stu4 = new student(194041204, "赵六", 97);
            os.writeObject(stu1);
            os.writeObject(stu2);
            os.writeObject(stu3);
            os.writeObject(stu4);
            Object o1 = is.readObject();
            System.out.println(o1);
            Object o2 = is.readObject();
            System.out.println(o2);
            Object o3 = is.readObject();
            System.out.println(o3);
            Object o4 = is.readObject();
            System.out.println(o4);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值