【问题标题】:How to print the result of a void type method into a file with Java PrintWriter如何使用 Java PrintWriter 将 void 类型方法的结果打印到文件中
【发布时间】:2022-01-02 11:42:09
【问题描述】:

我正在为我的 Java 编程课程做练习,问题是这样的:

编写一个名为 printTree 的方法,该方法根据传递给过程的高度值将星号三角形输出到文件中。在 main 方法中测试这个方法。

  • 例如高度为 3 的三角形应输出到名为 file14 的文件:

我只是不确定如何将 void 返回写入我在 main 方法中创建的文件。我想尽量减少除 FileWriter 之外的任何其他 java.io 方法的导入,但感谢您提供任何帮助。

import java.io.PrintWriter;

public class OutputToFile14 {

    public static void main(String[] args) throws Exception{
        
        //Creating PrintWriter
        PrintWriter output = new PrintWriter("file14.txt");
        
        //writing method output to file
        output.write(printTree(4));
        
        //saving file
        output.close();
    }

    public static void printTree (int height) throws IOException{
        
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < height; j++) {
                if (j < i) {
                    System.out.print("*");
                }
            }
            System.out.println();
        }
    } 
}

【问题讨论】:

  • 您可以让printTree 方法直接使用output 变量(将其作为参数传递)或让printTree 返回一个字符串
  • 如果指令如您所说,您不会返回任何结果,而是在方法中写入文件。
  • @HovercraftFullOfEels 我会改为在方法中编写 printwriter 语句吗?
  • 创建一个变量然后在函数中输出它是最好的方法。

标签: java file methods call printwriter


【解决方案1】:

四个观察。 System.outPrintStream (您可以将 PrintStream 传递给您的方法)。 try-with-Resources 允许您消除显式的 close() 调用。使用System.getProperty("user.home") 允许您直接写入主文件夹(这很方便)。并在您的内部循环中使用j &lt; i 而不是if (j &lt; i)。喜欢,

public static void main(String[] args) throws Exception {
    try (PrintStream output = new PrintStream(
            new File(System.getProperty("user.home"), "file14.txt"))) {
        printTree(output, 4);
    }
}

public static void printTree(PrintStream out, int height) throws IOException {
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < i; j++) {
            out.print("*");
        }
        out.println();
    }
}

此外,从 Java 11 开始,

public static void printTree(PrintStream out, int height) throws IOException {
    for (int i = 0; i < height; i++) {
        out.println("*".repeat(i)); // You might want "*".repeat(1 + i)
    }
}

【讨论】:

  • 更好的答案——谢谢
  • 我不太精通将对象作为参数发送给方法,但我假设与其他响应类似,这基本上使它成为“单线”:声明文件,命名它。然后它在打印流中使它可以作为参数发送?我很想听听一些清晰的观点,我的课程是在写作的早期阶段,所以我很想了解更多
  • System.outPrintStream。您可以构建自己的 PrintStream 实例。写信给File。有了这个constructor。至于发送一个对象作为参数main(String[] args)(这是你做的第一件事)。希望对您有所帮助。
  • @ElliottFrisch 谢谢!我认为这最终需要我做更多的研究工作,但我认为我会被引导到正确的方向。谢谢你的主要标题事实,我不知道,但现在知道了。非常感谢
【解决方案2】:

你可以这样解决

import java.io.PrintWriter;

public class OutputToFile14 {

    public static void main(String[] args) throws Exception{
        
        //Creating PrintWriter
        PrintWriter output = new PrintWriter("file14.txt");
        
        //writing method output to file
        output.write(printTree(4));
        
        //saving file
        output.close();
    }

    public static String printTree (int height) throws IOException{
        String output = "";
        
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < height; j++) {
                if (j < i) {
                    System.out.print("*");
                    output += "*";
                }
            }
            System.out.println();
            output += "\r\n";
        }
        return output;
    } 
}

这是一种有点难看的快速解决方法。

import java.io.PrintWriter;

public class OutputToFile14 {

    public static void main(String[] args) throws Exception{
        
        //Creating PrintWriter
        PrintWriter output = new PrintWriter("file14.txt");
        
        //writing method output to file
        //output.write(printTree(4));
        printTree(4, output);
        
        //saving file
        output.close();
    }

    public static void printTree (int height, PrintWriter pw) throws IOException{
        
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < height; j++) {
                if (j < i) {
                    System.out.print("*");
                    pw.write("*");
                }
            }
            System.out.println();
            pw.write("\r\n");
        }
    } 
}

【讨论】:

  • 非常感谢您的回答!尽管很抱歉,我想我会偏离这一点,因为我真的不知道如何将 PW 用作将来参考的参数。不过谢谢,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-28
相关资源
最近更新 更多