java autoflush,PrintWriter autoflush令人费解的逻辑

out - An output stream

autoFlush - A boolean; if true, the println, printf, or format methods

will flush the output buffer

out - The output stream to which values and objects will be printed

autoFlush - A boolean; if true, the output buffer will be flushed

whenever a byte array is written, one of the println methods is invoked,

or a newline character or byte ('\n') is written

What was the reason for changing autoflush logic between these classes?

Because they are always considered as identical except for encoding moments and "autoflush" without flushing on print() hardly corresponds to principle of least astonishment, silly bugs occur:

I created a PrintWriter with autoflush on; why isn't it autoflushing?

解决方案

I think the answer lies in the history of Java. The trio InputStream, OutputStream and PrintStream in java.io date back to Java 1.0. That is before serious support for file encodings and character sets were built into the language.

To quote the Javadoc:

"A PrintStream adds functionality to

another output stream, namely the

ability to print representations of

various data values conveniently. Two

other features are provided as well.

Unlike other output streams, a

PrintStream never throws an

IOException; instead, exceptional

situations merely set an internal flag

that can be tested via the checkError

method..."

To summarize, it is a convenience for generating textual output, grafted on top of lower level IO.

In Java 1.1, Reader, Writer and PrintWriter were introduced. Those all support character sets. While InputStream and OutputStream still had a real uses (raw data processing), PrintStream became far less relevant, because printing by nature is about text.

The Javadoc for PrintWriter explicitly states:

Unlike the PrintStream class, if

automatic flushing is enabled it will

be done only when one of the println()

methods is invoked, rather than

whenever a newline character happens

to be output. The println() methods

use the platform's own notion of line

separator rather than the newline

character.

Put another way, PrintWriter should only be used through the print*(...) APIs, because writing newline characters etc should not be the caller's responsibility, the same way dealing with file encodings and character sets are not the caller's responsibility.

I would argue that PrintWriter should have been java.io.Printer instead, and not have extended Writer. I don't know whether they extended to mimic PrintStream, or because they were stuck on maintaining the pipe design idiom.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值