How do I implement a serializableBufferedWriter in Java?

时间:2023-01-13 16:23:21

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.

我正在尝试扩展java.io.BufferedWriter,使其成为Serializable。我尝试简单地扩展BufferedWriter并实现Serializable接口。它有两个构造函数,所以代码看起来像这样。

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?

但是我在运行时得到一个没有效的构造函数异常。在这里读到我之后,为了实现Serializable的类,它的第一个非可序列化的超类需要有一个无参数的构造函数。那么如何使用扩展BufferedWriter并由SerializableBufferWriter扩展的无参数构造函数在中间添加一个类?

Any help is greatly appreciated

任何帮助是极大的赞赏

1 个解决方案

#1


3  

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.

您可以做的是存储要发送数据的文件名或URL。当您的应用程序重新加载此信息时,它会重新创建编写器。


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.

你遇到的问题不仅仅是类,而且你想保留的所有字段都是Serializable。大多数流不是Serializable,因此您需要为这些流进行自定义序列化。

So how do I add a class in between with a no-argument constructor that extends

那么如何在一个扩展的无参数构造函数之间添加一个类

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

添加一个不带参数的构造函数。你需要使用构造函数的参数调用super,这是很难的部分。

The biggest problem you have is that you are trying to Serialise something which isn't designed to be Serialized.

您遇到的最大问题是您正在尝试序列化一些非设计为序列化的东西。

#1


3  

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.

您可以做的是存储要发送数据的文件名或URL。当您的应用程序重新加载此信息时,它会重新创建编写器。


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.

你遇到的问题不仅仅是类,而且你想保留的所有字段都是Serializable。大多数流不是Serializable,因此您需要为这些流进行自定义序列化。

So how do I add a class in between with a no-argument constructor that extends

那么如何在一个扩展的无参数构造函数之间添加一个类

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

添加一个不带参数的构造函数。你需要使用构造函数的参数调用super,这是很难的部分。

The biggest problem you have is that you are trying to Serialise something which isn't designed to be Serialized.

您遇到的最大问题是您正在尝试序列化一些非设计为序列化的东西。