我在朋友写的代码基础上寻思好心加个类型判断再强转凉了

img

img


package com.itheima.convertedio;

import java.io.*;

public class ConvertedDemo5 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //1. 写对象
        //2. 修改JavaBean类
        //3. 读对象

        //写对象 --- 序列化
        //method1();

        //读对象 --- 反序列化
        method2();
    }

    private static void method2() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
        if (ois.readObject() instanceof User) {
            User user = (User) ois.readObject();
            System.out.println(user);
        }
        ois.close();
    }

    private static void method1() throws IOException {
        User user = new User("zhangsan", "qwer");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
        oos.writeObject(user);
        oos.close();
    }
}

img


if里面就读完了,下面又读了一次,有问题