如何打印的结果无效型方法进入一个文件与Java PrintWriter

0

的问题

我在做练习工作我Java然,问题转为这样:

写一个方法被称为printTree,输出的文件的一个三角形的星号的基础上的高度价值传递的程序。 测试这种方法的主要方法。

  • E.g。 三角形的,高度3应该输出到一个文件叫file14:

我只是不知道该怎么写的效回返的文件,我已经在主要方法。 我想尽量减少进口的任何其他java。io方法除了FileWriter,但任何帮助是理解的,谢谢。

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();
        }
    } 
}
call file java methods
2021-11-24 02:22:55
2

最好的答案

3

四点意见。 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();
    }
}

此外,由于Java11,

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)
    }
}
2021-11-24 02:40:07

一个更好的答案-谢谢你
Hovercraft Full Of Eels

我不太熟悉与发送中的一个目作为一个参数的方法,但我假设类似于其他反应,这基本上使其所有"一个衬里":宣布的文件、命名。 然后它正在打印流使其sendable作为一个争论? 我喜欢听到的一些清楚点,我当然是f在早期阶段的书面文件,因此我想了解更多
Achor Gabriel

System.out 是一个 PrintStream. 你可以构造你自己的 PrintStream 实例。 写到 File. 这个 构造. 至于 发送对象为参, main(String[] args) (这是第一件事,你做)。 希望这有所帮助。
Elliott Frisch

@ElliottFrisch谢谢你! 我觉得这最终将需要更多的研究工作在我的结束,但是,我认为我会指导的方向是正确的。 谢谢你,主要标题实际上,我不知道,但现在做的。 非常感激
Achor Gabriel
-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");
        }
    } 
}
2021-11-24 02:35:23

我赞赏大大你的答案! 我想我要远离这虽然对不起,我真的不知道如何与PW作为一个参数对今后参考。 谢谢你,非常赞赏。
Achor Gabriel

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................