java.io.StreamCorruptedException:无效的类型代码:AC [英] java.io.StreamCorruptedException: invalid type code: AC

查看:490
本文介绍了java.io.StreamCorruptedException:无效的类型代码:AC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从文件中读取一些对象。代码在第一次迭代中工作正常,在第二次迭代中,它给出了StreamCorruptedException。这里是我的代码,

  private ArrayList< Check> checks = null; 
ObjectInputStream ois = null;
尝试{
checks = new ArrayList< Check>(4);
ois = new ObjectInputStream(new FileInputStream(src\\easycheque\\data\\Templates.dat));
Object o = null;
尝试{
o = ois.readObject();
int i = 1; ($!$ null){
cheques.add((Check)o);
System.out.println(i ++); //打印迭代次数
o = ois.readObject(); //这里发生异常

catch(ClassNotFoundException ex){//用于ois readObject()
Logger.getLogger(TemplateReader.class.getName()).log(Level.SEVERE ,null,ex);
$ b $ catch(EOFException ex){//用于ois readObject()
//文件结尾达到停止读取
System.out.println(ois closed) ;
ois.close();

$ b catch(IOException ex){
Logger.getLogger(TemplateReader.class.getName()).log(Level.SEVERE,null,ex);
}

是异常的一部分。在打印之前,这个'1'被打印(因为sout)

  SEVERE:null 
java.io.StreamCorruptedException :无效的类型代码:java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)处的AC
在java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)处

我无法弄清楚为什么会发生这种情况。在很少的论坛帖子中,我发现在编写文件的过程中追加到文件时会发生这种情况。这是真正的原因吗? (在写作阶段,我正在追加档案)。
如果有,是否有正确的方法来读取附加文件?

这里是我用来写入文件的代码

  ObjectOutputStream objectOut = new ObjectOutputStream(new FileOutputStream(src\\easycheque\\data\\templates.dat,true)); 

objectOut.writeObject(check);
objectOut.flush();
objectOut.close();

书写不是一个迭代过程。


解决方案


(在写入阶段,我正在追加文件) / p>

这就是问题所在。您不能附加到ObjectOutputStream。但是我已经在SO上留下了这个问题的一个解决方案: / Append-to-an-objectoutputstream / 1195078#1195078> AppendableObjectOutputStream
$ b 编辑



在编写器中,我发现你写了一个一个检查对象并刷新并关闭了流。从读者中,我清楚地看到,您正在尝试阅读多个检查对象。你可以阅读第一个,但不是其他的。所以对我来说,很清楚,你重新打开了Stream并追加了越来越多的检查对象。这是不允许的。



您必须在一个会话中写入全部检查对象。或者使用AppendableObjectOutputStream而不是标准的ObjectOutputStream。

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

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.

Thank you :)

解决方案

(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.

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

EDIT

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.

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

这篇关于java.io.StreamCorruptedException:无效的类型代码:AC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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