如何处理无效的流标头异常:00000001? [英] How do I handle an Invalid Stream header Exception: 00000001?

查看:178
本文介绍了如何处理无效的流标头异常:00000001?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.io.StreamCorruptedException:无效的流标头:00000001简单项目

我发现了这一点,这似乎是一个常见问题。如果您写入包含文件的目录,然后在以后手动将其删除,则最终将收到此错误。

I found this, and this seems to be a common problem. If you write to a directory with files and then manually deleted one later, you will end up getting this error.

java.io.StreamCorruptedException: invalid stream header: 00000001
     at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:806)
     at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)

代码:

private void deserialize(File input){
    // Let's deserialize an Object
    System.out.println("Here");
    try {
                FileInputStream fileIn = new FileInputStream(input); 
                ObjectInputStream in = new ObjectInputStream(fileIn);
                    //System.out.println("Deserialized Data: \n" + ((Song)in.readObject()));
                    database.add((Song)in.readObject());
                in.close();
                fileIn.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Runner.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private void open(){
    for(File fIn: f.listFiles()){
        deserialize(fIn);
    }
}

所以发生了什么事对象称为Song。歌曲被序列化到我家图书馆的文件夹中。我可以看到文件,并且它们确实会完全反序列化。该程序运行没有问题,只是看到弹出的无效流头异常很烦人,而我不希望以后再遇到问题。

So whats going on is that there is a database of Objects called Song. Songs are serialized to a folder in my home library. I can see the files and they do get deserialized completely. The program has no problem running, it's just annoying to see the Invalid Stream Header Exception pop up and I don't want to have problems on later down the line.

除了在写入目录后不要触摸该目录之外,我如何处理该异常?

How do I deal with this exception other than "Don't touch that directory after it's written to"?

推荐答案

基本上,这意味着您要读取的文件不包含序列化对象。

Basically, it means that the file you are trying to read does not contain a serialized object.


除了在目录写入后不要触摸该目录之外,我该如何处理该异常?

How do I deal with this exception other than "Don't touch that directory after it's written to"?

另一个方式是:


  • 仅反序列化文件名是应用程序将使用的文件名。

  • Only deserialize files whose filename is one that your application would use.

过滤掉可能令人怀疑的特定文件名;例如在Mac上,为包含目录元数据的隐藏文件的名称。

Filter out specific filenames that are likely to be suspect; e.g. on Macs, the name of the hidden file that contains directory metadata.

捕获并忽略 StreamIgnoredException ,虽然有点冒险。 (文件可能是真正的序列化对象,由于其他一些原因而损坏了……您需要了解它。)

Catch and ignore the StreamIgnoredException, though that is a bit risky. (The file could be a genuine serialized object that is corrupted for some other reason ... and you need to know about it.)

顺便说一句,像您一样处理异常是一个坏主意。如果确实期望发生异常,则您不想打印堆栈跟踪。如果出乎意料,则建议停止加载目录并允许传播异常。至少,您需要向调用方报告加载有问题。 (另一方面,如果程序正在运行... erm ...,则用户不应面临堆栈跟踪。)

By the way, dealing with exceptions like you are doing is a bad idea. If the exception is genuinely expected, then you don't want to print a stacktrace. If it is unexpected, then it may be advisable to stop loading the directory, and allow the exception to propagate. At the very least, you need to report that the load had problems to the caller. (On the other hand, the user should not be confronted with stack traces if the program is ... erm ... functioning.)

这篇关于如何处理无效的流标头异常:00000001?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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