简体   繁体   English

如何在Java中序列化和反序列化kdtree

[英]How to serialize and deserialize a kdtree in java

I have a kdtree whose nodes consist of the following fields: public static class Node implements Serializable { 我有一个kdtree,它的节点包括以下字段:公共静态类Node实现Serializable {

    public int discriminator;
    public double value; 

    public Node leftChild;
    public Node rightChild;
    public DataPoint object;
    }

where DataPoint definition: 其中DataPoint定义:

public static class DataPoint implements Serializable { public Comparable X; 公共静态类DataPoint实现Serializable {public Comparable X; public Comparable Y; 可比Y public Comparable Z; 公开可比Z;

     public double latitude;
     public double longitude;

     public ArrayList<String> text = new ArrayList<String>(); 
     }

I want to serialize the tree, store in a file and deserialize while answering range queries. 我想序列化树,存储在文件中,然后在回答范围查询时反序列化。 My understanding of this concept od serialization is not fine. 我对这个概念和序列化的理解不太好。 From whatever I gathered,I have written the following functions, which is not working. 从我收集到的所有信息中,我编写了以下功能,这些功能不起作用。 Can anybody help? 有人可以帮忙吗?

 public static void serialize(final OutputStream out, kdtrees.Node node) throws IOException
 {
        if( node == null ) 
        {
            //out.write( "()".getBytes() );
            //write to an output leaf file
            out.write("#".getBytes());

        } 
        else 
        {
            //out.write( "(".getBytes() );
            ((ObjectOutputStream) out).writeObject(node);
            serialize( out, node.leftChild );
            serialize( out, node.rightChild );
            //out.write( ")".getBytes( ) );
        }
    }


 public static kdtrees.Node deserialize(final ObjectInputStream reader) throws IOException, ClassNotFoundException 
 {

                  StringTokenizer st = new StringTokenizer(reader.readObject().toString()); 
                  String curToken = st.nextToken().toString();
                  while(st.hasMoreTokens())
                  { 
                      if(curToken.equals("#".trim()))
                          return null;


                      kdtrees.Node e = (kdtrees.Node)reader.readObject();

                       e.leftChild = deserialize(reader);
                       e.rightChild = deserialize(reader);

                    return e;

                  }
               return null;   


    } 

Casting an arbitrary OutputStream to an ObjectOutputStream is just going to fail. 将任意OutputStream强制转换为ObjectOutputStream将会失败。 Instead, use new ObjectOutputStream(outputStream) to create a wrapper around the OutputStream , and the same thing for the InputStream s. 而是使用new ObjectOutputStream(outputStream)围绕OutputStream创建包装器,对于InputStream则使用相同的包装器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM