java 0d 0a_序列化:java.io.StreamCorruptedException:无效的流头:0AACED00

我是一名练习File

IO技能的学生,遇到使用ObjectInputStream从文件读取对象的问题。代码始终抛出InvalidClassException,但我找不到代码是如何将其在线或通过反复试验将其抛出的。这是我的代码:

import java.io.*;

import java.util.ArrayList;

import java.util.List;

public class ReadFromFile {

String filename;

List os;

public ReadFromFile(String filename) {

this.filename = filename;

os = new ArrayList<>();

}

public Object[] readObject() {

try {

FileInputStream fis = new FileInputStream(filename);

ObjectInputStream ois = new ObjectInputStream(fis);

System.out.print("reading\n");

while (true) {

os.add(ois.readObject());

System.out.print("read one\n");

}

} catch (EOFException e) {

return os.toArray();

} catch (FileNotFoundException e) {

System.out.print("File not found\n");

return os.toArray();

} catch (ClassNotFoundException e) {

System.out.print("Class not found\n");

return os.toArray();

} catch (StreamCorruptedException e) {

System.out.print("SC Exception\n");

e.printStackTrace();

return os.toArray();

} catch (InvalidClassException e) {

e.printStackTrace();

System.out.print("IC Exception\n");

return os.toArray();

} catch (OptionalDataException e) {

System.out.print("OD Exception\n");

return os.toArray();

} catch (IOException e) {

System.out.print("IO Exception\n");

return os.toArray();

}

}

}

我编写了所有单独的catch块,以弄清楚抛出了什么异常,并且该异常总是抛出InvalidClassException。

这也是我的树类:

import java.io.Serializable;

public class Tree implements Serializable {

private static final long serialVersionUID = -310842754445106856L;

String species;

int age;

double radius;

public Tree() {

this.species = null;

this.age = 0;

this.radius = 0;

}

public Tree(String species, int age, double radius) {

this.species = species;

this.age = age;

this.radius = radius;

}

public String toString() {

return species + ", age: " + age + ", radius: " + radius;

}

}

这是我的写文件功能:

public boolean write(Object object) {

try {

File f = new File(filename);

FileOutputStream fos = new FileOutputStream(f,true);

ObjectOutputStream oos = new ObjectOutputStream(fos);

oos.writeObject(object + "\n");

oos.close();

} catch (FileNotFoundException e) {

System.out.print("File Not Found\n");

return false;

} catch (IOException e) {

System.out.print("IOException\n");

return false;

}

return true;

}

感谢您的知识…

堆栈跟踪:

SC Exception

java.io.StreamCorruptedException: invalid stream header: 0AACED00

at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:806)

at java.io.ObjectInputStream.(ObjectInputStream.java:299)

at ReadFromFile.readObject(ReadFromFile.java:17)

at WriteAndRecord.main(WriteAndRecord.java:21)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:497)

at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

流程结束,退出代码为0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值