Java 我试图将这段代码的输出显示到另一个文件中;output.txt“;在我的代码中,但它仍然没有';不行。它仍然显示在控制台上

Java 我试图将这段代码的输出显示到另一个文件中;output.txt“;在我的代码中,但它仍然没有';不行。它仍然显示在控制台上,java,algorithm,Java,Algorithm,我搞不懂最后一部分。有人能帮我吗。如何编写代码以创建名为“output.txt”的新输出文件,并在“output.txt”中显示输出。我尝试了4种不同的方法,但我仍然无法理解。我也在网上看过多篇文章,我只是感到困惑 import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.FileWriter; import java.io.IOException; /

我搞不懂最后一部分。有人能帮我吗。如何编写代码以创建名为“output.txt”的新输出文件,并在“output.txt”中显示输出。我尝试了4种不同的方法,但我仍然无法理解。我也在网上看过多篇文章,我只是感到困惑

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;

//class to represent the AVL tree node
class Node {
  int key, height;
  Node left, right;

  Node(int d) {
    key = d;
    height = 1;
  }
}

//public class to represent an AVLTree
public class Main {
  Node root;
  int height(Node N) {
    if (N == null)
      return 0;
    
    return N.height;
  }

  // method to find maximum of two numbers
  int max(int a, int b) {
    return (a > b) ? a : b;
  }

  // a method to perform right rotation
  Node rightRotate(Node y) {
    Node x = y.left;
    Node T2 = x.right;
    
    // Perform rotation
    x.right = y;
    y.left = T2;

    // Update heights
    y.height = max(height(y.left), height(y.right)) + 1;
    x.height = max(height(x.left), height(x.right)) + 1;
    
    // Return new root
    return x;
  }

  // a method for left rotation
  Node leftRotate(Node x) {
    Node y = x.right;
    Node T2 = y.left;
    
    // Perform rotation
    y.left = x;
    x.right = T2;
    
    // Update heights
    x.height = max(height(x.left), height(x.right)) + 1;
    y.height = max(height(y.left), height(y.right)) + 1;

    // Return new root
    return y;
  }

  // returns balance factor of a node
  int getBalance(Node N) {
    if (N == null)
      return 0;
      
    return height(N.left) - height(N.right);
  }

  Node insert(Node node, int key) {
    if (node == null)
      return (new Node(key));
    if (key < node.key)
      node.left = insert(node.left, key);
    else if (key > node.key)
      node.right = insert(node.right, key);
    
    node.height = 1 + max(height(node.left),height(node.right));
    int balance = getBalance(node);

    // if |balace| > 1 then we perform rotation
    // rotation type is based on the value of balance factor

    if (balance > 1 && key < node.left.key)
      return rightRotate(node);
    if (balance < -1 && key > node.right.key)
      return leftRotate(node);
    if (balance > 1 && key > node.left.key) {
      node.left = leftRotate(node.left);
      return rightRotate(node);
    }
    if (balance < -1 && key < node.right.key) {
      node.right = rightRotate(node.right);
      return leftRotate(node);
    }
    return node;
  }

  //method to print the level order of tree
  void printLevelOrder() {
    int h = root.height;
    int i;

    //print all levels of tree
    for (i=1; i<=h; i++) {
      printLevel(root, i);
      System.out.println();
    }
  }

  // method print a level
  void printLevel (Node root ,int level) {
    if (root == null)
      return;

    if (level == 1)
      System.out.print("( "+ root.key + " , "+ root.height +", "+ getBalance(root) +")");
    else if (level > 1) {
      printLevel(root.left, level-1);
      printLevel(root.right, level-1);
    }
  }

  //main method
  public static void main(String[] args) throws IOException {
    //declare a empty tree
    Main tree = new Main();
    //read a input file and insert all the values in AVL tree root
    Scanner scanner = new Scanner(new File("input.txt"));
    FileWriter fw = new FileWriter(new File("output.txt"));
    while(scanner.hasNextInt()) {
      int x = scanner.nextInt();
      //insert x into AVL tree
      tree.root = tree.insert(tree.root, x);
    }

    //Now print the level order of tree
    tree.printLevelOrder();
  }
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
导入java.io.FileWriter;
导入java.io.IOException;
//类来表示AVL树节点
类节点{
int键,高度;
左、右淋巴结;
节点(int d){
key=d;
高度=1;
}
}
//表示AVLTree的公共类
公共班机{
节根;
整数高度(节点N){
如果(N==null)
返回0;
返回N.高度;
}
//求两个数的最大值的方法
最大整数(整数a,整数b){
返回(a>b)?a:b;
}
//执行右旋转的方法
节点右旋转(节点y){
节点x=y.左;
节点T2=x右;
//轮换
x、 右=y;
y、 左=T2;
//更新高度
y、 高度=最大值(高度(y左),高度(y右))+1;
x、 高度=最大值(高度(x左)、高度(x右))+1;
//返回新根
返回x;
}
//左旋转的一种方法
节点左旋转(节点x){
节点y=x,右;
节点T2=y.左;
//轮换
y、 左=x;
x、 右=T2;
//更新高度
x、 高度=最大值(高度(x左)、高度(x右))+1;
y、 高度=最大值(高度(y左),高度(y右))+1;
//返回新根
返回y;
}
//返回节点的平衡因子
int getBalance(节点N){
如果(N==null)
返回0;
返回高度(N.左)-高度(N.右);
}
节点插入(节点,int键){
if(node==null)
返回(新节点(键));
if(键<节点键)
node.left=插入(node.left,键);
else if(key>node.key)
node.right=插入(node.right,键);
node.height=1+max(高度(node.left)、高度(node.right));
int balance=getBalance(节点);
//如果| balace |>1,则执行旋转
//旋转类型基于平衡因子的值
如果(平衡>1&&keynode.right.key)
返回leftRotate(节点);
if(balance>1&&key>node.left.key){
node.left=leftRotate(node.left);
返回右旋转(节点);
}
if(余额<-1&&key<节点右键){
node.right=rightRotate(node.right);
返回leftRotate(节点);
}
返回节点;
}
//方法打印树的级别顺序
作废printLevelOrder(){
int h=根高度;
int i;
//打印所有级别的树
对于(i=1;i 1){
打印级别(root.left,级别1);
打印级别(root.right,级别1);
}
}
//主要方法
公共静态void main(字符串[]args)引发IOException{
//声明一棵空树
主目录树=新的主目录();
//读取输入文件并在AVL树根中插入所有值
Scanner Scanner=新扫描仪(新文件(“input.txt”);
FileWriter fw=新的FileWriter(新文件(“output.txt”);
while(scanner.hasNextInt()){
int x=scanner.nextInt();
//将x插入AVL树
tree.root=tree.insert(tree.root,x);
}
//现在打印树的级别顺序
tree.printLevelOrder();
}
}

您创建了一个
文件编写器
,但从未使用过它,您仍然使用
System.out
将输出打印到控制台

相反,您需要使用文件输出。这里有一种方法可以使用
PrintStream
(与
System.out
System.err
相同的类来最小化对代码的更改)。首先,您的输出方法需要将
PrintStream
作为参数,以便它们可以将其用于输出:

  //method to print the level order of tree
  void printLevelOrder(PrintStream out) {
    int h = root.height;
    int i;

    //print all levels of tree
    for (i=1; i<=h; i++) {
      printLevel(root, i, out);

      out.println();
    }
  }

  // method print a level
  void printLevel(Node root, int level, PrintStream out) {
    if (root == null)
      return;

    if (level == 1)
      out.print("( "+ root.key + " , "+ root.height +", "+ getBalance(root) +")");
    else if (level > 1) {
      printLevel(root.left, level-1, out);
      printLevel(root.right, level-1, out);
    }
  }

您创建了一个
FileWriter fw
,但从未使用过它。您的代码仍在使用
System.out.print
,而不是您创建的
FileWriter
    //Now print the level order of tree
    tree.printLevelOrder(System.out); // print to console

    PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
    tree.printLevelOrder(out); // print to file