问题
I am pretty new to java and wanted to understand the reasoning why this does not work. Why does the sys out print work perfectly, but the buffered writer does not? I am just trying to understand the difference between the two/
//print the input matrix to the user
System.out.println("Matrix read: ");
System.out.println("------------------" +
"---------------------");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.printf("%5d ", a[i][j]);
bw.write(a[i][j]);
bw.flush();
}
//print a blank line
System.out.println();
buffered writer output (from .txt file):
The Determinant is: 5
The Determinant is: 3
�The Determinant is: 64
� �� ���The Determinant is: 270
������ ���The Determinant is: 0
�������� ����The Determinant is: 270
������ The Determinant is: 0
The Determinant is: 0
Sys out print output:
Matrix read:
---------------------------------------
5
---------------------------------------
Matrix read:
---------------------------------------
2 3
---------------------------------------
5 9
---------------------------------------
Matrix read:
---------------------------------------
3 -2 4
---------------------------------------
-1 5 2
---------------------------------------
-3 6 4
---------------------------------------
回答1:
Instead of writing a raw int
with
bw.write(a[i][j]);
(if you want the same thing written to your BufferedWriter
) you need to format your output the same way. You could use String.format
and something like
bw.write(String.format("%5d ", a[i][j]));
And, you'll need to add a new line (where you call System.out.println
) if you want it to be identical, like
bw.write(System.lineSeparator());
回答2:
With write()
you write bytes. In your first matrix you have the numerical value 5. If you use write(5)
, you write the byte value 5 which is a non-printable character that the text editor you are using is displaying somehow. If you use a hexdump utility, you will see that it acutally is the byte 5
.
To the printf
function you five a format string with which you tell it how to format the argument, which is the number 5
. %5d
here means format the number as string with at least 5 width, filled with spaces in front. and then a space afterwards.
If you want to have the same effect on your buffered writer, wrap it in a PrintWriter
and use the same printf
method and arugments and you will get the same result. If you only want to format the number as String in the BufferedWriter
, you have to convert your number to a String befory you write it, e. g. with Integer.toString(5).getBytes()
and then write this byte array with the according write()
method.
回答3:
System.out
is a PrintStream
; to get the same behavior, try using a PrintStream
(or PrintWriter
) instantiated with your file name and use the print
/println
/format
methods.
The BufferedWriter.write(int c)
methods interprets the int
parameter as a character, so if you pass 32
, it's going to print a space character; that's most likely not what you want here.
来源:https://stackoverflow.com/questions/38416078/buffered-writer-vs-sys-out-print