Reputation: 15
i want to store list in file, i used the following code: as path
is the path of file store list in it
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)
Upvotes: 0
Views: 941
Reputation: 718758
The problem is that only objects that implement the Serializable
interface can be saved to a file that way. 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. 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. And a Socket
cannot be serialized because it is (in general) impossible to reestablish a broken socket connection.
In your case, your BloomFilter
objects contain direct or indirect references to a 3rd-party Bitset
class which is not serializable. If you look at the source code (e.g. 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
instead1. 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
replace the 3rd-party BitSet
with java.util.BitSet
... which is serializable.
1 - This was clearly a deliberate design decision. The javadocs call this out as an advantage of this re-implementation of the standard BitSet
class.
Upvotes: 3