如何在不获取 java.io.StreamCorruptedException: invalid type code: AC 的情况下附加到 ObjectInputStream? [英] How to append to an ObjectInputStream without getting java.io.StreamCorruptedException: invalid type code: AC?

查看:19
本文介绍了如何在不获取 java.io.StreamCorruptedException: invalid type code: AC 的情况下附加到 ObjectInputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文件中读取一些对象.该代码在第一次迭代中运行良好,在第二次迭代时它给出了 StreamCorruptedException.这是我的代码,

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,

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

以下是例外的一部分.打印之前打印这个'1'(因为sout)

below is part of the exception. Before printing this '1' is printed (because of the 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.

谢谢:)

推荐答案

(我在写入阶段追加到文件中)

(I am appending to the file during the writing phase)

这就是问题所在.您不能附加到 ObjectOutputStream.这肯定会破坏流,并且您会收到 StreamCorruptedException.

And that is the problem. You can't append to an ObjectOutputStream. That will definitly corrupt the stream and you get StreamCorruptedException.

但我已经在 SO 上留下了这个问题的解决方案:一个 AppendableObjectOutputStream

But I already left a solution to this problem on SO: an AppendableObjectOutputStream

编辑

从作者那里,我看到您编写了一个检查对象并刷新并关闭流.从读者那里,我清楚地看到,您正在尝试阅读多个检查对象.您可以阅读第一个,但不能阅读其余部分.所以对我来说很清楚,你重新打开 Stream 并附加越来越多的检查对象.这是不允许的.

From the writer I see that you write one cheque object and flush and close the stream. From the reader, I clearly see, that you're trying to read more than one cheque 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.

您必须在一个会话"中编写所有检查对象.或者使用 AppendableObjectOutputStream 而不是标准的 ObjectOutputStream.

You have to write all cheque objects in 'one session'. Or use the AppendableObjectOutputStream instead of the standard ObjectOutputStream.

这篇关于如何在不获取 java.io.StreamCorruptedException: invalid type code: AC 的情况下附加到 ObjectInputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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