Java:如何使这个序列化?

标签 java serialization outputstream

我需要以下类是可序列化的。

package helpers;

public class XY implements Comparable<XY>
{
    public int x;
    public int y;

    public XY (int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public int compareTo( XY other ) 
    {
        String compare1 = this.x + "-" + this.y;
        String compare2 = other.x + "-" + other.y;

        return compare1.compareTo( compare2 );
    }

    public String toString()
    {
        return this.x + "-" + this.y;
    }

}

截至目前,我无法将其作为具有输出流的对象发送。我已尝试实现可序列化,但没有成功。

最佳答案

实现 Serializable 达到目的,但您必须使用 ObjectOutputStream 编写对象,而不仅仅是一个 OutputStream。

关于Java:如何使这个序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8116147/

相关文章:

java - 从起点、终点和半径绘制圆弧

java - Android Google+ 使用 WebView 登录

c++ - boost 空 std::forward_list 的序列化

java - Java中如何关闭OutputStream列表?

java - 编辑二进制文件中的特定字节 - Java

java - 从具有相同持久性单元的多个数据库中读取?

java - 如何使用 Spring ClassPathResource : with classpath: or classpath*: and leading/or not?

php - 基于表单的数组

c++ - 通过 MPI 发送 Lambda 闭包

c# - 如何在 C# 中读取 Response.OutputStream