在 PrintWriter 中,为什么 print() 函数不自动刷新?
In PrintWriter, why doesn't the print() function also auto-flush?
查看以下构造函数的 PrintWriter
合同时:
public PrintWriter(OutputStream out, boolean autoFlush)
Creates a new PrintWriter
from an existing OutputStream
. This convenience constructor creates the necessary intermediate OutputStreamWriter
, which will convert characters into bytes using the default character encoding.
Parameters:
out
- An output stream
autoFlush
- A boolean
; if true
, the println
, printf
, or format
methods will flush the output buffer
See Also:
OutputStreamWriter.OutputStreamWriter(java.io.OutputStream)
请注意 autoFlush
标志仅适用于 println
、printf
和 format
。现在,我知道 printf
和 format
基本上做与 print
完全相同的事情,除了有更多选项,但我只是不明白为什么他们不包括 [=28= 】 合同中也有。他们为什么做出这个决定?
我怀疑这是因为 Java 作者对性能做出了假设:
考虑以下代码:
public static void printArray(int[] array, PrintWriter writer) {
for(int i = 0; i < array.length; i++) {
writer.print(array[i]);
if(i != array.length - 1) writer.print(',');
}
}
您几乎肯定不希望这种方法在每次调用后调用 flush()
。这可能会对性能造成很大影响,尤其是对于大型阵列。而且,如果出于某种原因你确实想要那个,你可以 只需调用 flush
你自己 .
想法是 printf
、format
和 println
方法可能会同时打印大量文本,因此在之后刷新是有意义的每个人。但它很少(如果有的话)在仅 1 个或几个字符后刷新。
经过一番搜索,I have found a citation for this reasoning(强调我的):
Most of the examples we've seen so far use unbuffered I/O. This means each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive.
To reduce this kind of overhead, the Java platform implements buffered I/O streams. Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.
<snip>
It often makes sense to write out a buffer at critical points, without waiting for it to fill. This is known as flushing the buffer.
Some buffered output classes support autoflush, specified by an optional constructor argument. When autoflush is enabled, certain key events cause the buffer to be flushed. For example, an autoflush PrintWriter
object flushes the buffer on every invocation of println
or format
.
查看以下构造函数的 PrintWriter
合同时:
public PrintWriter(OutputStream out, boolean autoFlush)
Creates a new
PrintWriter
from an existingOutputStream
. This convenience constructor creates the necessary intermediateOutputStreamWriter
, which will convert characters into bytes using the default character encoding.Parameters:
out
- An output stream
autoFlush
- Aboolean
; iftrue
, theprintln
,printf
, orformat
methods will flush the output bufferSee Also: OutputStreamWriter.OutputStreamWriter(java.io.OutputStream)
请注意 autoFlush
标志仅适用于 println
、printf
和 format
。现在,我知道 printf
和 format
基本上做与 print
完全相同的事情,除了有更多选项,但我只是不明白为什么他们不包括 [=28= 】 合同中也有。他们为什么做出这个决定?
我怀疑这是因为 Java 作者对性能做出了假设:
考虑以下代码:
public static void printArray(int[] array, PrintWriter writer) {
for(int i = 0; i < array.length; i++) {
writer.print(array[i]);
if(i != array.length - 1) writer.print(',');
}
}
您几乎肯定不希望这种方法在每次调用后调用 flush()
。这可能会对性能造成很大影响,尤其是对于大型阵列。而且,如果出于某种原因你确实想要那个,你可以 只需调用 flush
你自己 .
想法是 printf
、format
和 println
方法可能会同时打印大量文本,因此在之后刷新是有意义的每个人。但它很少(如果有的话)在仅 1 个或几个字符后刷新。
经过一番搜索,I have found a citation for this reasoning(强调我的):
Most of the examples we've seen so far use unbuffered I/O. This means each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive.
To reduce this kind of overhead, the Java platform implements buffered I/O streams. Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.
<snip>
It often makes sense to write out a buffer at critical points, without waiting for it to fill. This is known as flushing the buffer.
Some buffered output classes support autoflush, specified by an optional constructor argument. When autoflush is enabled, certain key events cause the buffer to be flushed. For example, an autoflush
PrintWriter
object flushes the buffer on every invocation ofprintln
orformat
.