提问人:Achor Gabriel 提问时间:11/24/2021 最后编辑:SSpokeAchor Gabriel 更新时间:11/24/2021 访问量:639
如何使用 Java PrintWriter 将 void 类型方法的结果打印到文件中
How to print the result of a void type method into a file with Java PrintWriter
问:
我正在为我的 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();
}
}
}
答:
-2赞
SSpoke
11/24/2021
#1
你可以这样解决它
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");
}
}
}
评论
0赞
Achor Gabriel
11/24/2021
非常感谢您的回答!我想我会偏离这一点,尽管很抱歉,因为我真的不知道如何使用 PW 作为将来参考的参数。谢谢你,非常感谢。
3赞
Elliott Frisch
11/24/2021
#2
四点意见。 是一个 PrintStream
(你可以将 a 传递给你的方法)。 允许您消除显式调用。使用允许您直接写入主文件夹(这很方便)。而不是在您的内部循环中使用。喜欢System.out
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)
}
}
评论
0赞
Hovercraft Full Of Eels
11/24/2021
一个更好的答案 -- 谢谢
0赞
Achor Gabriel
11/24/2021
我不太精通将对象作为方法的参数发送,但我假设与其他响应类似,这基本上使它完全“单行”:声明文件,命名它。然后它在打印流中使它可以作为参数发送?我很想听到一些清晰的观点,我的课程处于写作申请的早期阶段,所以我很想了解更多
0赞
Elliott Frisch
11/24/2021
System.out
是一个 PrintStream
。您可以构建自己的实例。要写入 .有了这个构造函数。至于发送一个对象作为参数,(这是你做的第一件事)。希望能有所帮助。PrintStream
File
main(String[] args)
0赞
Achor Gabriel
11/24/2021
@ElliottFrisch 谢谢!我认为这最终将需要更多的研究工作,但我认为我会被引导到正确的方向。谢谢你提供的主要标题事实,我不知道,但现在知道了。非常感谢
评论
printTree
output
printTree