PrintWriter class 未按预期工作

PrintWriter class not working as expected

我有一个将信息存储到二叉树中的程序。

我正在尝试使用 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);
    }  
}

我不清楚你要做什么。正如 darioscity 指出的那样,主要问题是您每次都在创建一个新的 PrintWriter 实例;因此覆盖。 你可以看到你实际上在写一些东西:你树的第一个节点(因为它是你写的最后一个节点,因此这个特定的节点没有被覆盖)在文件中。在到达那个之前,你一直在写一些其他的东西(其他节点)并且每次都覆盖它。

public void print() throws IOException {
    PrintWriter writer = new PrintWriter(new FileWriter("Contacts.txt"), true);

    print(root, writer);
}

private void print(Node root, PrintWriter file) throws IOException {

    if (root != null) {
        file.write(root.inputData.firstName + "  " + root.inputData.lastName + "  "
                + root.inputData.address + "  " + root.inputData.phoneNumber + "\n");
        print(root.leftChild, file);

        print(root.rightChild, file);
    }
    file.close();
}

}

请注意,使用该代码(idk,如果这是您想要的),您并不是在编写每个节点。