从文件中加载对象的 ArrayList [重复]

Posted

技术标签:

【中文标题】从文件中加载对象的 ArrayList [重复]【英文标题】:Loading ArrayList of Objects from file [duplicate] 【发布时间】:2013-04-03 20:00:35 【问题描述】:

我一直在尝试在 Java 中保存和加载对象,但我在加载文件时遇到了一些问题。我在这里要做的是加载一个名为 Employee 的对象的 ArrayList,我已经对其进行了序列化。当我尝试加载它时,我仍然遇到一堵红色的错误墙。我究竟做错了什么?这是我的代码:

try (FileInputStream filStrom = new FileInputStream(filpeker);
                        ObjectInputStream objektStrom = new ObjectInputStream(filStrom))
                    
                    employeelist = (ArrayList<Employee>)objektStrom.readObject();
                    objektStrom.close();
                 catch (IOException | ClassNotFoundException e) 
                    JOptionPane.showMessageDialog(null, "Feil under lesing av fil! " + e.getMessage());
                    e.printStackTrace();
                 

我遇到的错误是:

java.io.InvalidClassException: ovinger9.Employee; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(Unknown Source)
at java.io.ObjectStreamClass.checkDeserialize(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.readObject(Unknown Source)
at ovinger9.EmployeeGUI$2.actionPerformed(EmployeeGUI.java:135)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

【问题讨论】:

对我来说看起来很明确:ovinger9.Employee; 没有有效的构造函数 @JimGarrison ...newInvalidClassException... Employee类中确保你有默认构造函数,还要注意如果你有一个重写的构造函数,那么你必须显式添加默认构造函数。 听起来像是编组/解组框架的工作——它们可以处理纯文本和 XML 无参数构造函数不一定是序列化的要求。 【参考方案1】:

将不带参数的公共构造函数添加到ovinger9.Employee 类(除了您已经拥有的另一个构造函数)。

【讨论】:

+1 敏锐的眼光和良好的记忆力...【参考方案2】:

如果您的 Employee 类扩展了一个不是 Serializable 的类,并且超类没有零参数构造函数,则可能会发生这种情况,如 java.io.Serializable 文档中所述。为了解决这个问题,要么将你的超类声明为Serializable,要么给超类添加一个零参数的构造函数。

【讨论】:

我根据您的建议解决了,谢谢!我完全忘记了我没有序列化的 Employee 超类。

以上是关于从文件中加载对象的 ArrayList [重复]的主要内容,如果未能解决你的问题,请参考以下文章

无法在python中加载json文件[重复]

在 Servlet/JSP 中加载属性文件 [重复]

是否可以从资产文件夹中加载可绘制对象?

Flexigrid从对象中加载数据

从一个巨大的(> 200MB)数组文件中加载随机对象而不加载整个数组

在 python 中加载 JSON 作为实际对象,而不是 dict [重复]