序列化:java.io.StreamCorruptedException:无效的流标题:0AACED00 [英] Serialization:java.io.StreamCorruptedException: invalid stream header: 0AACED00

查看:221
本文介绍了序列化:java.io.StreamCorruptedException:无效的流标题:0AACED00的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名学习我的文件IO技能的学生,我遇到了使用ObjectInputStream从文件中读取对象的问题。代码一直抛出一个InvalidClassException,我无法找到代码是如何在网上抛出或通过反复试验。这是我的代码:

  import java.io. *; 
import java.util.ArrayList;
import java.util.List;

public class ReadFromFile {
String filename;
List< Object>操作系统;

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\\\
);
while(true){
os.add(ois.readObject());
System.out.print(read one\);
}
} catch(EOFException e){
return os.toArray();
} catch(FileNotFoundException e){
System.out.print(File not found\\\
);
返回os.toArray();
} catch(ClassNotFoundException e){
System.out.print(Class not found\\\
);
返回os.toArray();
} catch(StreamCorruptedException e){
System.out.print(SC Exception\\\
);
e.printStackTrace();
返回os.toArray();
} catch(InvalidClassException e){
e.printStackTrace();
System.out.print(IC Exception\\\
);
返回os.toArray();
} catch(OptionalDataException e){
System.out.print(OD Exception\\\
);
返回os.toArray();
} catch(IOException e){
System.out.print(IO Exception\\\
);
返回os.toArray();
}
}
}

我写了所有单独的catch块来弄清楚抛出了什么异常,它总是抛出InvalidClassException。



这里也是我的树类:

  import java.io.Serializable; 

公共类树实现Serializable {
private static final long serialVersionUID = -310842754445106856L;
字符串种类;
int age;
双半径;

public Tree(){
this.species = null;
this.age = 0;
this.radius = 0;
}
public Tree(String species,int age,double radius){
this.species = species;
this.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\\\
);
返回false;
} catch(IOException e){
System.out.print(IOException\\\
);
返回false;
}
返回true;
}

您的知识值得赞赏...



堆栈跟踪:

  SC异常
java.io.StreamCorruptedException:无效的流标题:0AACED00
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:806)
at java.io.ObjectInputStream。< init>(ObjectInputStream.java:299)
at ReadFromFile.readObject( ReadFromFile.java:17)
在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)
在com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

处理完成后退出代码0

解决方案

  java.io.S treamCorruptedException:无效的流标题:0AACED00 

这是由于附加到 FileOutputStream引起的。正如我在上面的评论中提到的,你不能附加到 ObjectOutputStream 写的流,至少在没有特殊措施的情况下。保持文件 ObjectOutputStream 打开,直到您编写了要写入的所有对象,然后关闭它,然后从中反序列化。 / p>

NB正如我所提到的,

  while((object = in .readObect())!= null)

不是有效的对象读取循环。 readObject()在流结束时不返回null:它抛出 EOFException 。只要你编写一个,就可以在流中的任何地方出现 null 。循环的正确形式是:

 尝试
{
for(;;)
{
Object object = in.readObject();
// ...
}
}
catch(EOFException exc)
{
//流结束
}
//其他捕获块...

NB 2这个:

  oos.writeObject(object +\ n); 

应该只是

  oos.writeObject(对象); 

否则你无意中调用 toString()并且毫无意义地附加一个行终止符,因此 readObject()的结果将是一个String,而不是原始对象。


I'm a student practicing my File IO skills and I am coming up against a problem with reading Objects from a file using ObjectInputStream. The code is consistently throwing an InvalidClassException and I can't find how the code is throwing it online or by trial and error. Here's my code:

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ReadFromFile {
String filename;
List<Object> 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();
    }
}
} 

I wrote all of the separate catch blocks to figure out what Exception was being thrown and it always throws the InvalidClassException.

Here also is my Tree Class:

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;
}
}

And here is my write to file function:

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;
}

Your knowledge is appreciated...

Stack trace:

SC Exception
java.io.StreamCorruptedException: invalid stream header: 0AACED00
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:806)
at java.io.ObjectInputStream.<init>(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)

Process finished with exit code 0

解决方案

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

This is caused by appending to the FileOutputStream. As I mentioned in a comment above, you can't append to a stream written by ObjectOutputStream, at least not without special measures. Keep the file and the ObjectOutputStream open until you've written all the objects you want to write, then close it, then deserialize from it.

NB As I also mentioned,

while ((object = in.readObect()) != null)

is not a valid object-reading loop. readObject() doesn't return null at end of stream: it throws EOFException. null can occur anywhere in the stream, any time you write one. The correct form of the loop is:

try
{
    for (;;)
    {
        Object object = in.readObject();
        // ...
    }
}
catch (EOFException exc)
{
    // end of stream
}
// other catch blocks ...

NB 2 This:

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

should be just

oos.writeObject(object);

Otherwise you're implicity calling toString() and pointlessly appending a line terminator, so the result of readObject() will be a String, not the original object.

这篇关于序列化:java.io.StreamCorruptedException:无效的流标题:0AACED00的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆