如何在不获取 java.io.StreamCorruptedException: invalid type code: AC 的情况下附加到 ObjectInputStream?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3182240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to append to an ObjectInputStream without getting java.io.StreamCorruptedException: invalid type code: AC?
提问by Niroshan
I am trying to read some objects from a file. The code works fine for the first iteration and at the second iteration it gives a StreamCorruptedException. Here is my code,
我正在尝试从文件中读取一些对象。该代码在第一次迭代中运行良好,在第二次迭代时它给出了 StreamCorruptedException。这是我的代码,
private ArrayList<Cheque> cheques = null;
ObjectInputStream ois = null;
try {
cheques = new ArrayList<Cheque>(4);
ois = new ObjectInputStream(new FileInputStream("src\easycheque\data\Templates.dat"));
Object o = null;
try {
o = ois.readObject();
int i=1;
while (o != null) {
cheques.add((Cheque) o);
System.out.println(i++); // prints the number of the iteration
o = ois.readObject(); // exception occurs here
}
} catch (ClassNotFoundException ex) {// for ois readObject()
Logger.getLogger(TemplateReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (EOFException ex) {// for ois readObject()
// end of the file reached stop reading
System.out.println("ois closed");
ois.close();
}
} catch (IOException ex) {
Logger.getLogger(TemplateReader.class.getName()).log(Level.SEVERE, null, ex);
}
below is part of the exception. Before printing this '1' is printed (because of the sout)
以下是例外的一部分。在打印这个'1'之前打印(因为sout)
SEVERE: null
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
I can't figure out why this is happening. In few forum posts, I came across that this happens when you append to a file during writing it. Is it the real reason?. (I am appending to the file during writing phase). If so is there a proper way to read an appended file?
我不明白为什么会这样。在一些论坛帖子中,我发现在编写文件时附加到文件时会发生这种情况。这是真正的原因吗?。(我在写入阶段附加到文件中)。如果是这样,是否有正确的方法来读取附加文件?
here is the code i use to write to the file
这是我用来写入文件的代码
ObjectOutputStream objectOut = new ObjectOutputStream(new FileOutputStream("src\easycheque\data\templates.dat", true));
objectOut.writeObject(cheque);
objectOut.flush();
objectOut.close();
writing is not an iterative process.
写作不是一个迭代的过程。
Thank you :)
谢谢 :)
采纳答案by Andreas Dolk
(I am appending to the file during the writing phase)
(我在写入阶段附加到文件中)
And that is the problem. You can't append to an ObjectOutputStream. That will definitly corrupt the stream and you get StreamCorruptedException.
这就是问题所在。您不能附加到 ObjectOutputStream。这肯定会破坏流,并且您会收到 StreamCorruptedException。
But I already left a solution to this problem on SO: an AppendableObjectOutputStream
但是我已经在 SO 上留下了这个问题的解决方案:一个AppendableObjectOutputStream
EDIT
编辑
From the writer I see that you write onecheque object and flush and close the stream. From the reader, I clearly see, that you're trying to read more than onecheque object. And you can read the first one but not the rest. So to me it is perfectly clear, that you reopen the Stream and append more and more cheque objects. Which is not allowed.
从作者那里,我看到您编写了一个检查对象并刷新并关闭了流。从读者那里,我清楚地看到,您正在尝试阅读多个支票对象。您可以阅读第一个,但不能阅读其余部分。所以对我来说很清楚,你重新打开 Stream 并附加越来越多的检查对象。这是不允许的。
You have to write allcheque objects in 'one session'. Or use the AppendableObjectOutputStream instead of the standard ObjectOutputStream.
您必须在“一个会话”中编写所有检查对象。或者使用 AppendableObjectOutputStream 而不是标准的 ObjectOutputStream。
回答by sfussenegger
Creating a new ObjectInputStream without closing the underlying FileInputStream solves this problem:
在不关闭底层 FileInputStream 的情况下创建一个新的 ObjectInputStream 解决了这个问题:
FileInputStream fin = new FileInputStream(file);
while (...) {
ObjectInputStream oin = new ObjectInputStream(fin);
Object o = oin.readObject();
...
}
fin.close();