【发布时间】:2015-03-07 06:43:16
【问题描述】:
java.io.StreamCorruptedException: invalid stream header: 00000001 Simple Project
我发现了这个,这似乎是一个常见问题。如果您写入包含文件的目录,然后手动删除一个,您最终会收到此错误。
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 的对象数据库。歌曲被序列化到我的家庭图书馆中的一个文件夹中。我可以看到这些文件,它们确实被完全反序列化了。程序运行没有问题,只是看到 Invalid Stream Header Exception 弹出很烦人,我不想在以后遇到问题。
除了“写入后不要触摸该目录”之外,我该如何处理这个异常?
【问题讨论】:
标签: java serialization deserialization objectinputstream