gpt4 book ai didi

java - java中如何将对象存储在文件中?

转载 作者:行者123 更新时间:2023-12-01 19:54:07 27 4
gpt4 key购买 nike

我想将列表存储在文件中,我使用了以下代码:因为path是文件存储列表在其中的路径

public void writeToFile(String path,List<BloomFilter> s) {
try(ObjectOutputStream write=new ObjectOutputStream(new FileOutputStream(path)))
{
write.writeObject(s);
}
catch(FileNotFoundException nse)
{
nse.printStackTrace();
}
catch(IOException eio)
{
eio.printStackTrace();
}
}

但它抛出以下错误:

java.io.NotSerializableException: com.googlecode.javaewah.datastructure.BitSet
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at java.util.ArrayList.writeObject(ArrayList.java:766)
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:1128)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at searchencrypted.ReadData.writeToFile(ReadData.java:247)
at searchencrypted.GUI.jButton4ActionPerformed(GUI.java:446)
at searchencrypted.GUI.access$300(GUI.java:38)
at searchencrypted.GUI$4.actionPerformed(GUI.java:173)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

最佳答案

问题是只有实现了Serialized接口(interface)的对象才能以这种方式保存到文件中。这传递适用于正在序列化的对象图中的所有类......除非您采取措施来处理它。

标准类库中的某些类实现Serialized,因此无法被序列化。通常,这有充分的技术原因。例如,Thread 无法序列化,因为无法序列化线程的状态。并且 Socket 无法序列化,因为(通常)不可能重新建立断开的套接字连接。

就您而言,您的 BloomFilter 对象包含对不可序列化的第 3 方 Bitset 类的直接或间接引用。如果您查看源代码(例如 http://grepcode.com/file/repo1.maven.org/maven2/com.googlecode.javaewah/JavaEWAH/1.0.1/com/googlecode/javaewah/datastructure/BitSet.java/ ),您会发现那个 BitSet 实现了 Externalized 而不是1。所以......您也许能够修改代码以在适当的时候使用外部化机制。

或者:

  • 将引用 BitSet 的字段标记为 transient,以便它不会被序列化,或者

  • 将第 3 方 BitSet 替换为可序列化的 java.util.BitSet ...。

<小时/>

1 - 这显然是一个经过深思熟虑的设计决定。 javadoc 将此称为重新实现标准 BitSet 类的一个优点。

关于java - java中如何将对象存储在文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50185215/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com