我無法弄清楚如何編寫一個二叉搜索樹來遞歸地文件。我在Tree類中打開一個BufferWriter,並將文件也寫入Wrtie。然後,我將BufferWriter發送給Node類以遍歷樹並寫入文件。但它不起作用。寫入文件。 (二叉搜索樹)
public void write(String filePath)
{
if(root != null) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(filePath));
root.write(out);
} catch (IOException e) {
}
}
}
public void write(BufferedWriter out)
{
if (this.getLeft() != null) this.getLeft().write(out);
out.write(this.data());
if (this.getRight() != null) this.getRight().write(out);
}
請澄清「它不起作用!」你在期待什麼,你看到了什麼? – 2010-04-22 20:00:36
考慮處理'IOException',即使只有一行到'System.err'或等價物。不太可能,因爲你會得到一個異常,如果你的代碼無法正常工作,它就會沉默地吞噬異常。 – Etaoin 2010-04-22 20:01:48