0

我有一个 kdtree,其节点包含以下字段: public static class Node implements Serializable {

    public int discriminator;
    public double value; 

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

其中数据点定义:

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

     public double latitude;
     public double longitude;

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

我想序列化树,存储在文件中并在回答范围查询时反序列化。我对这个概念 od 序列化的理解并不好。从我收集的任何内容中,我编写了以下函数,但这些函数不起作用。有人可以帮忙吗?

 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;   


    } 
4

1 回答 1

1

将任意值OutputStream转换为 anObjectOutputStream只会失败。相反,用于围绕, 和snew ObjectOutputStream(outputStream)创建一个包装器。OutputStreamInputStream

于 2013-01-05T18:24:00.683 回答