6

我一直在尝试做一个需要可附加 ObjectOutputStream 的小项目。我已经经历了几个解决方案,我发现这个最初似乎解决了我的问题。但是在我的项目的进一步发展中,我开始遇到意外的异常。以下是我的课。

public class PPAccount implements Serializable
{
    private Profile profile;
    private String email;
    private float accountBal;
    private boolean isActivated;
    private String activationCode;
    private ArrayList<Transaction> transactions;

    //a few functions   
}
public class PPRestrictedAccount extends PPAccount {
    private String parentEmail;
    private float withdrawLimit;

        //a few functions
}
public class PPBusinessAccount extends PPAccount {
    private ArrayList <PPRestrictedAccount> accountOperators;

        //a few functions
}
public class PPStudentAccount extends PPAccount {
    private String parentEmail;

        //a few functions
}

我观察到的是,使用this我覆盖了 ObjectOutputStream 并在我将对象附加到文件时使用它。但是如果我写会发生什么:

PPBusinessAccount首先,重复任意次数......然后写PPAccount一切都很好。 PPAccount首先,重复....然后写PPBusinessAccount然后写PPAccount,它写得很好,但是在阅读时我得到了ClassCastException

我尝试读取对象并将它们直接存储在类的实例中Object以避免类强制转换但仍然readObject()抛出ClassCastException

我尽力描述我的场景,告诉你是否没有得到任何东西。为什么会这样??它与它第一次编写的标题有关吗?沿着基类标题不能支持子类?什么转机?

我正在做这样的演员:

Object o = ois.readObject();        //Surprisingly exception is raised here (line:50 in DataStore)
PPAccount ppa = (PPAccount)o;

堆栈跟踪

java.lang.ClassCastException: java.lang.String cannot be cast to java.io.ObjectStreamClass
    at java.io.ObjectInputStream.readClassDesc(Unknown Source)
    at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
    at java.io.ObjectInputStream.readClassDesc(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at java.util.ArrayList.readObject(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
    at java.io.ObjectInputStream.readSerialData(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
    at java.io.ObjectInputStream.readSerialData(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at in.msitprogram.iiit.paypal.persistance.DataStore.lookupAccount(DataStore.java:50)
    at in.msitprogram.iiit.paypal.persistance.DataStore.writeAccount(DataStore.java:131)
    at in.msitprogram.iiit.paypal.console.PPNewAccountScreen.show(PPNewAccountScreen.java:78)
    at in.msitprogram.iiit.paypal.console.MainMenu.show(MainMenu.java:42)
    at in.msitprogram.iiit.paypal.PPSystem.main(PPSystem.java:17)
Exception in thread "main" java.lang.NullPointerException
    at in.msitprogram.iiit.paypal.persistance.DataStore.lookupAccount(DataStore.java:66)
    at in.msitprogram.iiit.paypal.persistance.DataStore.writeAccount(DataStore.java:131)
    at in.msitprogram.iiit.paypal.console.PPNewAccountScreen.show(PPNewAccountScreen.java:78)
    at in.msitprogram.iiit.paypal.console.MainMenu.show(MainMenu.java:42)
    at in.msitprogram.iiit.paypal.PPSystem.main(PPSystem.java:17)

在写入lookUpAccount流时从流中读取writeAccount,这里是代码:

public static PPAccount lookupAccount(String email) throws IOException, ClassNotFoundException 
    {
        PPAccount account = null; //initialize it after reading from file
        // write code to open the files, read
        PPAccount foundAccount=null;
        ObjectInputStream ois=null;
        FileInputStream fis=null;
        File ff = new File(PPConstants.AllAccountDetails);
        if(!ff.exists())
        {
            //System.out.println("Required file not found");
            return null;
        }
        try
        {
            fis=new FileInputStream(PPConstants.AllAccountDetails);
            ois = new ObjectInputStream(fis);
            while(fis.available()>0 && foundAccount==null)
            {
                //Object o=null;
                PPAccount ppa=null;
                try
                {
                    ppa = (PPAccount)ois.readObject();
                    if(ppa==null)
                        return null;
                    System.out.println(ppa);
                }

                catch(ClassCastException cce)
                {
                    System.out.println("Class cast exception "+cce.getCause());
                    cce.printStackTrace();  
                }
                if(email.equals(ppa.getEmail()))
                {
                    foundAccount=ppa;
                    break;
                }
                if(ppa instanceof PPBusinessAccount)
                {
                    PPBusinessAccount ppba = (PPBusinessAccount)ppa;
                    ArrayList<PPRestrictedAccount> alist=ppba.getAccountOperators();
                    if(alist==null)
                        continue;
                    Iterator<PPRestrictedAccount> it = alist.iterator();
                    while(it.hasNext())
                    {
                        PPRestrictedAccount ppr=(PPRestrictedAccount) it.next();
                        System.out.println(ppr);
                        if(email.equals(ppr.getEmail()))
                        {
                            foundAccount = ppr;
                            break;
                        }
                    }//iterators while loop
                }//if it is a businessAccount
            }//outer while  
        }//try
        finally
        {
            if(ois!=null)
                ois.close();
            if(fis!=null)
                fis.close();
        }   
        return foundAccount;
    }
    public static void writeAccount(PPAccount account,Boolean append) throws IOException, ClassNotFoundException, DuplicateAccountException
    {
        ObjectOutputStream oos=null;
        FileOutputStream fos=null;
        try
        {
            if(!append)
            {
                fos= new FileOutputStream(PPConstants.AllAccountDetails);
                oos = new ObjectOutputStream(fos);
                //System.out.println("Not Appending");
                oos.writeObject(account);
            }
            else
            {
                File ff = new File(PPConstants.AllAccountDetails);
                if(!ff.exists())
                {
                    System.out.println("Required file not found");
                    return;
                }
                PPAccount aa=lookupAccount(account.getEmail());
                if(aa!=null)
                    throw new DuplicateAccountException("An Account already exits with this email-ID");
                oos = new AppendingObjectOutputStream(new FileOutputStream(PPConstants.AllAccountDetails,append));
                oos.writeObject(account);
            }
        }
        finally
        {
            if(oos!=null)
                oos.close();
            if(fos!=null)
                fos.close();
        }

    }
4

3 回答 3

13

这里的问题是,之前给你附加的海报让ObjectOutputStream你误入歧途。ObjectOutputStream/尝试ObjectInputStream只存储每个对象一次,然后再引用已存储的对象。也就是说,如果你有一堆同一个类的对象,你可以在流中得到这样的结果:

CLASS_1_DESCRIPTION
OBJECT_1
REF_TO_CLASS_1
OBJECT_2
REF_TO_CLASS_1
OBJECT_3
...

当 anObjectInputStream将流转换回一堆对象时,它会维护一个列表,其中列出了它已经反序列化的内容。它告诉你的错误是它试图反序列化一个对象,读取应该是对对象类描述的引用,但是当它在其内部表中查找该引用时,它看到了一个String. 很自然地,它爆发了。

我认为修复就像这样简单 - 在您的 中AppendableObjectOutputStream,更改此方法:

  @Override
  protected void writeStreamHeader() throws IOException {
    // do not write a header, but reset the handle list
    reset();
  }

中的reset()方法在ObjectOutputStream流中插入一个标记,说“此时丢弃所有状态”。然后,当您使用 a 重新读取此内容时ObjectInputStream,输入流对已反序列化的内容的想法将与输出流认为的状态相匹配,当它首先反序列化这些内容时。

(编辑:从评论中回答问题)

我能想到的唯一不利后果是:

  • 最终文件将比您将所有内容都写入 one 时更长ObjectOutputStream,尤其是在同一个Profile对象出现多次的情况下。即使没有,您也会在流中重复类描述符,因此大量重复 {open AppendableObjectOutputStream, write one object, close stream} 可能会使文件大小有点膨胀。

  • 与此相关的是,在反序列化所有内容后,您可能会得到应该是相同对象的多个副本。例如,假设您编写了一堆东西,包括一些PPRestrictedAccount对象,然后关闭流,将其打开为AppendableObjectOutputStream,并写出 aPPBusinessAccount在其operators列表中包含PPRestrictedAccount您之前写出的一些 s。当您重新阅读所有内容时,您最初阅读的 s 与您在's列表中找到的 sPPRestrictedAccount将不是相同的对象(也就是说,它们不会是==) 。它们将被单独实例化。为避免这种情况,您需要使用一种方法对它们进行重复数据删除。写入单个的所有内容PPRestrictedAccountPPBusinessAccountoperatorsreadResolveAppendableObjectOutputStream但是,实例将正确连接。根据您的应用程序,这可能根本不用担心。

就是否会爆炸的安全性而言,这与 java 序列化的任何其他使用一样安全;您的课程没有任何特定的内容可以使其正常工作。请注意,在输出文件的多个单独开口中写入的任何对象都将被反序列化为原始对象的单独副本。(没有任何readResolve魔法)

于 2012-09-15T14:19:32.457 回答
0

试试这样......

readObject() 返回 objectsof类型 Object,因此您需要将它们显式转换为其原始类型...

例如:

PPAccount pa = (PPAccount) readObject();

于 2012-09-05T10:23:22.497 回答
0

没有明确的答案,但有一些想法和事情可以尝试:

  • 堆栈跟踪包括at java.util.ArrayList.readObject(Unknown Source),这表明在反序列化包含ArrayList. 通过注释掉你的问题private ArrayList<Transaction> transactions;

  • 如果您在不使用 appender的情况下生成单个文件,您是否有同样的问题?如果是这样,请创建两种相同内容的形式:一种带有 appender,一种没有。差异?

  • 我确实看到另一个类似问题的参考,没有解决方案。也使用相同的附加程序:序列化/反序列化 ClassCastException: x cannot be cast to java.io.ObjectStreamClass

于 2012-09-14T06:11:20.233 回答