I am trying to extend java.io.BufferedWriter so make it Serializable. I tried simply extending BufferedWriter and implementing Serializable interface. It has two constructors so the code looks like this.
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 I get an no valid constructor exception at run time. After reading around I here that for a class to implement Serializable, it’s first non-serializable super class needs to have a no-argument constructor. So how do I add a class in between with a no-argument constructor that extends BufferedWriter and is extended by SerializableBufferWriter?
Any help is greatly appreciated
What you can do is store the name of a file or a URL to send data to. When your application reloads this information, it recreates the writer.
The problem you have is that not just the class, but all the field you want to keep much be Serializable as well. Most streams are not Serializable so you need to have a custom serialization for these.
Add a constructor which takes no arguments. You need to call super with arguments you constructor which is the hard part.
The biggest problem you have is that you are trying to Serialise something which isn’t designed to be Serialized.