我创建了一个类,并使用Serializable定期将此类的对象保存到内部存储。当我想要检索这些对象时,我使用这种方法:
for(File i: getFilesDir().listFiles()){
//Skipped enclosing try-catch block
FileInputStream fis = openFileInput(i.getName());
ObjectInputStream ois = new ObjectInputStream(fis);
SingleWallet theObjectWeWant = ois.readObject();
}
这会导致编译时错误(不兼容的类型)。如何从object
返回的ois.readObject()
恢复课程?我做错了什么?
答案 0 :(得分:1)
即使我使用listFiles()获取目录中的所有文件,它们也表示为文件,而不是卡片对象。
那是因为他们是文件,而不是对象。
答案 1 :(得分:-1)
The mistake I made was failing to cast my newly gotten object to my intended class.
Changing
SingleWallet theObjectWeWant = ois.readObject();
to
SingleWallet theObjectWeWant = (SingleWallet) ois.readObject();
made the difference.
Basically, I should have casted from object to SingleWallet.
I hope this helps someone out there.