quick question, I don't know what I'm missing here. If I have a console output of 9 numbers in a row, and 9 total rows, how would I go about writing that exact output to an external text file, so it looks the same way in the text file.
快速问题,我不知道我在这里缺少什么。如果我有一个连续9个数字的控制台输出,总共9行,我将如何将该确切输出写入外部文本文件,因此它在文本文件中的外观相同。
Assuming the console looks like this:
假设控制台看起来像这样:
2 4 5 1 9 3 6 8 7
9 7 6 2 5 8 4 1 3
1 8 3 7 4 6 9 2 5
8 5 2 3 1 9 7 4 6
3 1 9 4 6 7 8 5 2
7 6 4 5 8 2 3 9 1
4 3 8 6 2 5 1 7 9
5 9 7 8 3 1 2 6 4
6 2 1 9 7 4 5 3 8
And that console output is stored in an array variable named "myArray" How would I write it to a text file so it looks just like that (or separated by commas)
并且该控制台输出存储在名为“myArray”的数组变量中。如何将其写入文本文件,使其看起来就像那样(或用逗号分隔)
So far I have this:
到目前为止我有这个:
File solutionFile = new File("output.txt");
FileOutputStream stream = new FileOutputStream(solutionFile);
PrintStream writeOut = new PrintStream(stream);
System.setOut(writeOut);
for (int rows = 0; rows < 9; rows++) {
for (int columns = 0; columns < 9; columns++) {
System.out.println(myArray[rows][columns] + " ");
}
}
When it writes to the file, each number is placed on its own line. Any assistance possible? Thank you!
当它写入文件时,每个数字都放在它自己的行上。有可能吗?谢谢!
3 个解决方案
#1
2
Don't make the print statement a println
, just a print
.
不要使print语句成为println,只是打印。
for (int rows = 0; rows < 9; rows++) {
for (int columns = 0; columns < 9; columns++) {
System.out.print(myArray[rows][columns] + " "); //keep printing on the same line
}
System.out.println(); //go to the next line
}
Another thing you could do is I/O redirection. If you run the program via terminal or command prompt, you can type java MyProgram > outputFile.txt
. The >
redirects the console output to go to outputFile.txt
instead of where it normally goes.
您可以做的另一件事是I / O重定向。如果通过终端或命令提示符运行程序,则可以键入java MyProgram> outputFile.txt。 >重定向控制台输出以转到outputFile.txt而不是通常的位置。
#2
1
Use following code (instead of println use just pring and call println() after second for)
使用以下代码(而不是println只使用pring并在第二次之后调用println())
File solutionFile = new File("output.txt");
FileOutputStream stream = new FileOutputStream(solutionFile);
PrintStream writeOut = new PrintStream(stream);
System.setOut(writeOut);
for (int rows = 0; rows < 9; rows++) {
for (int columns = 0; columns < 9; columns++) {
System.out.print(myArray[rows][columns] + " ");
}
System.out.println();
}
#3
0
I know this is probably not something you need, but just to give you an idea about the other ways to do it, I put it here. The other answer is pretty much what you need.
我知道这可能不是你需要的,但只是为了让你了解其他方法,我把它放在这里。另一个答案就是你需要的。
If you are using command line to execute your java code, you can redirect STDOUT to a file by running your code like this: java Main > output.txt
如果使用命令行执行java代码,可以通过运行以下代码将STDOUT重定向到文件:java Main> output.txt
You can also redirect STDIN: java Main < input.txt > output.txt
.
您还可以重定向STDIN:java Main
#1
2
Don't make the print statement a println
, just a print
.
不要使print语句成为println,只是打印。
for (int rows = 0; rows < 9; rows++) {
for (int columns = 0; columns < 9; columns++) {
System.out.print(myArray[rows][columns] + " "); //keep printing on the same line
}
System.out.println(); //go to the next line
}
Another thing you could do is I/O redirection. If you run the program via terminal or command prompt, you can type java MyProgram > outputFile.txt
. The >
redirects the console output to go to outputFile.txt
instead of where it normally goes.
您可以做的另一件事是I / O重定向。如果通过终端或命令提示符运行程序,则可以键入java MyProgram> outputFile.txt。 >重定向控制台输出以转到outputFile.txt而不是通常的位置。
#2
1
Use following code (instead of println use just pring and call println() after second for)
使用以下代码(而不是println只使用pring并在第二次之后调用println())
File solutionFile = new File("output.txt");
FileOutputStream stream = new FileOutputStream(solutionFile);
PrintStream writeOut = new PrintStream(stream);
System.setOut(writeOut);
for (int rows = 0; rows < 9; rows++) {
for (int columns = 0; columns < 9; columns++) {
System.out.print(myArray[rows][columns] + " ");
}
System.out.println();
}
#3
0
I know this is probably not something you need, but just to give you an idea about the other ways to do it, I put it here. The other answer is pretty much what you need.
我知道这可能不是你需要的,但只是为了让你了解其他方法,我把它放在这里。另一个答案就是你需要的。
If you are using command line to execute your java code, you can redirect STDOUT to a file by running your code like this: java Main > output.txt
如果使用命令行执行java代码,可以通过运行以下代码将STDOUT重定向到文件:java Main> output.txt
You can also redirect STDIN: java Main < input.txt > output.txt
.
您还可以重定向STDIN:java Main