Moncel
2022-05-14 21:22
采纳率: 100%
浏览 46
已结题

JAVA 使用ObjectOutputStream 出现 java.io.StreamCorruptedException异常 为什么出现如何解决

测试代码

package ObjectIO;

import java.io.*;

public class ObjIO {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\Users\\IdeaProjects\\Basic\\Module_01\\src\\basic\\module01\\File\\c.txt"));
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\IdeaProjects\\Basic\\Module_01\\src\\basic\\module01\\File\\c.txt"));
        oos.writeObject(new Person(18,"ARY"));
        Object o = ois.readObject();
        System.out.println(o);
        oos.flush();
        oos.close();
        ois.close();
    }
}


Person类代码

package ObjectIO;

import java.io.Serializable;

public class Person implements Serializable {
    private int age;
    private String name;

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        System.out.println(name + age);
        return name + age;
    }
}


出现异常

img

1条回答 默认 最新

相关推荐 更多相似问题