[英]how store object in file in java?
i want to store list in file, i used the following code: as path
is the path of file store list in it 我想将列表存储在文件中,我使用了以下代码:因为
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();
}
}
but it throw the following error: 但它引发以下错误:
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)
The problem is that only objects that implement the Serializable
interface can be saved to a file that way. 问题在于,只有实现
Serializable
接口的对象才能以这种方式保存到文件中。 And this applies transitively to all classes in the graph of objects being serialized ... unless you take steps to deal with it. 除非您采取措施进行处理,否则这将可传递地应用于要序列化的对象图中的所有类。
Some classes in the standard class library don't implement Serializable
and therefore can't be serialized. 标准类库中的某些类未实现
Serializable
,因此无法进行序列化。 Typically there are sound technical reasons for this. 通常,有合理的技术原因。 For example, a
Thread
cannot be serialized because it is impossible to serialize the state of a thread. 例如,
Thread
无法序列化,因为不可能序列化线程的状态。 And a Socket
cannot be serialized because it is (in general) impossible to reestablish a broken socket connection. Socket
无法序列化,因为(通常)不可能重新建立断开的套接字连接。
In your case, your BloomFilter
objects contain direct or indirect references to a 3rd-party Bitset
class which is not serializable. 在您的情况下,您的
BloomFilter
对象包含对不可序列化的第三方Bitset
类的直接或间接引用。 If you look at the source code (eg http://grepcode.com/file/repo1.maven.org/maven2/com.googlecode.javaewah/JavaEWAH/1.0.1/com/googlecode/javaewah/datastructure/BitSet.java/ ) you will see that that BitSet
implements Externalizable
instead 1 . 如果您查看源代码(例如http://grepcode.com/file/repo1.maven.org/maven2/com.googlecode.javaewah/JavaEWAH/1.0.1/com/googlecode/javaewah/datastructure/BitSet.java / ),您将看到的是
BitSet
实现Externalizable
,而不是1。 So ... you may be able to modify your code to use the externalization mechanism at the appropriate point. 因此...您可以在适当的时候修改您的代码以使用外部化机制。
Alternatively: 或者:
mark the field that refers to the BitSet
as transient
so that it doesn't get serialized, or 将引用
BitSet
的字段标记为transient
,以使其不会序列化,或者
replace the 3rd-party BitSet
with java.util.BitSet
... which is serializable. 将第3方
BitSet
替换为可序列化的java.util.BitSet
...。
1 - This was clearly a deliberate design decision. 1-这显然是一个有意的设计决定。 The javadocs call this out as an advantage of this re-implementation of the standard
BitSet
class. javadocs将此称为重新实现标准
BitSet
类的一个优点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.