[英]How to serialize and deserialize a kdtree in java
我有一個kdtree,它的節點包括以下字段:公共靜態類Node實現Serializable {
public int discriminator;
public double value;
public Node leftChild;
public Node rightChild;
public DataPoint object;
}
其中DataPoint定義:
公共靜態類DataPoint實現Serializable {public Comparable X; 可比Y 公開可比Z;
public double latitude;
public double longitude;
public ArrayList<String> text = new ArrayList<String>();
}
我想序列化樹,存儲在文件中,然后在回答范圍查詢時反序列化。 我對這個概念和序列化的理解不太好。 從我收集到的所有信息中,我編寫了以下功能,這些功能不起作用。 有人可以幫忙嗎?
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;
}
將任意OutputStream
強制轉換為ObjectOutputStream
將會失敗。 而是使用new ObjectOutputStream(outputStream)
圍繞OutputStream
創建包裝器,對於InputStream
則使用相同的包裝器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.