簡體   English   中英

如何使用 Java PrintWriter 將 void 類型方法的結果打印到文件中

[英]How to print the result of a void type method into a file with Java PrintWriter

我正在為我的 Java 編程課程做練習,問題是這樣的:

編寫一個名為 printTree 的方法,該方法根據傳遞給過程的高度值將星號三角形輸出到文件。 在 main 方法中測試此方法。

  • 例如,高度為 3 的三角形應該 output 到名為 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();
        }
    } 
}

四個觀察。 System.out是一個PrintStream (您可以將PrintStream傳遞給您的方法)。 try-with-Resources允許您消除顯式的close()調用。 使用System.getProperty("user.home")允許您直接寫入主文件夾(這很方便)。 並在您的內部循環中使用j < i而不是if (j < 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)
    }
}

你可以這樣解決

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");
        }
    } 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM