我有一个将信息存储到二叉树中的程序。我正在尝试将所有信息打印到文本文件上。我正在使用在以前的情况下为我工作过的PrintWriter,但是这次我没有运气。必须通过对象在节点中调用信息。我已经调试并确定节点在树中的位置运行正常,因此问题出在打印到文件上。最后要注意的是,如果我通过System.out打印到控制台,它将完美地打印到控制台。任何可以帮助的人,我将不胜感激。
import java.io.*;
public class GALABST
{
public static void main(String[] args) throws IOException
{
Tree directory = new Tree();
InputData newPerson1 = new InputData("luca", "galati", "asdasda", "sadfasdf");
directory.insert(newPerson1);
InputData newPerson2 = new InputData("sdfasf", "blackman", "asdasda", "sadfasdf");
directory.insert(newPerson2);
InputData newPerson3 = new InputData("fsdgdfg", "kramer", "asdasda", "sadfasdf");
directory.insert(newPerson3);
InputData newPerson4 = new InputData("dsafgas", "wallace", "asdasda", "sadfasdf");
directory.insert(newPerson4);
InputData newPerson5 = new InputData("asdfasdfasdf", "dangelo", "asdasda",
"sadfasdf");
directory.insert(newPerson5);
InputData newPerson6 = new InputData("sfasdasfas", "alla", "asdasda", "sadfasdf");
directory.insert(newPerson6);
InputData newPerson7 = new InputData("hfdhsds", "eeves", "asdasda", "sadfasdf");
directory.insert(newPerson7);
File outputFile = new File ("Contacts.txt");
outputFile.delete();
directory.print();
}
}
class InputData
{
String firstName, lastName, address, phoneNumber;
InputData (String fN, String lN, String a, String pN)
{
firstName = fN;
lastName = lN;
address = a;
phoneNumber = pN;
}
}
class Tree
{
private Node root;
class Node
{
InputData inputData;
Node leftChild, rightChild;
Node(InputData iD)
{
inputData = iD;
this.leftChild = null;
this.rightChild = null;
}
}
void insert(InputData inputData)
{
root = insert(root, inputData);
}
Node insert(Node root, InputData inputData)
{
if (root == null)
{
root = new Node(inputData);
return root;
}
if (root.inputData.lastName.compareTo(inputData.lastName) < 0)
{
root.rightChild = insert(root.rightChild, inputData);
}
else if (root.inputData.lastName.compareTo(inputData.lastName) > 0)
{
root.leftChild = insert(root.leftChild, inputData);
}
else
{
if (root.inputData.firstName.compareTo(inputData.firstName) < 0)
{
root.rightChild = insert(root.rightChild, inputData);
}
else if (root.inputData.firstName.compareTo(inputData.firstName) > 0)
{
root.leftChild = insert(root.leftChild, inputData);
}
else
{
System.out.println("Info already present in contacts");
}
}
return root;
}
public void print() throws IOException
{
print(root);
}
private void print(Node root) throws IOException
{
PrintWriter writer = new PrintWriter(new FileWriter("Contacts.txt"), true);
if(root != null)
{
print(root.leftChild);
writer.write(root.inputData.firstName + " " + root.inputData.lastName + " " +
root.inputData.address + " " + root.inputData.phoneNumber);
print(root.rightChild);
}
writer.close();
}
}
print
方法的问题是,为每个写入文件第一行的调用创建一个新的PrintWriter
实例,因此在每个PrintWriter
实例关闭时,它都会覆盖文件的第一行。一种避免此行为的方法是创建一个PrintWriter
并将其作为参数传递,如下所示:
public void print() throws IOException
{
PrintWriter writer = new PrintWriter(new FileWriter("Contacts.txt"), true);
print(root, writer);
writer.close();
}
private void print(Node root, PrintWriter writer) throws IOException
{
if(root != null)
{
print(root.leftChild, writer);
writer.write(root.inputData.firstName + " " + root.inputData.lastName + " " +
root.inputData.address + " " + root.inputData.phoneNumber + "\n"); <-- added \n for clarity
print(root.rightChild, writer);
}
}