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

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

问题描述

我正在尝试从文件中读取一些对象.该代码在第一次迭代中运行良好,而在第二次迭代中,它给出了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"已打印(由于输出)

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并追加越来越多的check对象.不允许.

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的情况下追加到ObjectInputStream:无效的类型代码:AC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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