How to implement serializableBufferedWriter in Java?

I am trying to extend java.io.BufferedWriter to make it Serializable . I tried just extending the BufferedWriter and implementing the Serializable interface. It has two constructors, so the code is as follows.

 class SerializableBufferedWriter extends BufferedWriter implements Serializable{ /** * */ private static final long serialVersionUID = 1625952307886764541L; public SerializableBufferedWriter(OutputStreamWriter osw, int size){ super(osw, size); } public SerializableBufferedWriter(OutputStreamWriter osw){ super(osw); } } 

But at runtime, I get an invalid constructor exception. After reading here, for a class to implement Serializable , for it, the non-serializable superclass must first have a constructor with no arguments. So, how do I add a class between a constructor with no arguments that extends BufferedWriter and extends to SerializableBufferWriter ?

Any help is appreciated

+4
source share
1 answer

What you can do is save the file name or URL for sending data. When your application reloads this information, it re-creates the author.


The problem is that not only the class, but all the fields that you want to keep can also be Serializable. Most threads are not Serializable, so you need to have special serialization for them.

So, how do I add a class between the constructor without an argument that extends

Add a constructor that takes no arguments. You need to call super with the arguments you create, which is the hard part.

The biggest problem that you are facing is that you are trying to serialize something that is not meant to be serialized.

+3
source

All Articles