我一直在尝试做一个需要可追加ObjectOutputStream的小项目。 我已经经历了几个解决方案,我发现this它似乎首先解决了我的问题。但是在我的项目的进一步发展中,我开始意想不到的例外。 以下是我的课程。当追加对象OutputStream时出现ClassCastException
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
。
我尽力描述我的情况,告诉你是否没有得到任何东西。这是为什么发生?它是否与第一次编写的标题有关?沿着Base类头的行不能支持子类?什么是转身?
我做这样的演员:
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();
}
}
你可以发布异常跟踪吗?它也给出了什么是错误的明确线索... – SiB
你是否正在写入其他任何东西到流?例如,使用writeObject()以外的任何方法? – EJP
@EJP不,我没有写任何其他的'writeObject()'这是写入这些类中的任何一个的唯一方法。 – sasidhar