我正在尝试编写几种方法来将二叉树保存到文件中。它们工作正常,除了一个小问题。问题是在创建的每个文件的开头都插入了一个空行。为什么 PrintWriter 会这样做,我该如何防止它发生?
这是我的代码:
import java.io.*;
void callSaveGame(String fileName) throws IOException {
PrintWriter out = new PrintWriter(new FileWriter("X:\\path\\to\\save\\directory\\" + fileName)); // associate PrintWriter with file
saveGame(root, out);
out.close();
}
void saveGame(Node current, PrintWriter out) {
if (current != null) {
out.println(current.getData()); // print node to file
saveGame(current.getLChild(), out); // call for left child(ren)
saveGame(current.getRChild(), out); // call for right child(ren)
}
}