java - PrintWriter 类未按预期工作

标签 java printing binary-tree binary-search-tree file-writing

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

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

关于java - PrintWriter 类未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61443352/

相关文章:

algorithm - 对树的基本操作感到困惑

c++ - 寻找二叉树的深度

Java 类型-细化映射

java - while 循环不打印消息

java - Spring data Elasticsearch 按字段查找和最新日期查询

javascript - 为什么显示:inline-block prevent printing across multiple pages?

printing - JasperReports 页脚仅打印在最后一页

java - 使用 JavaFX 打印节点

创建固定大小的二叉树

java - 自动化 UML2 类图