PrintWriter autoflush令人费解的逻辑 [英] PrintWriter autoflush puzzling logic

查看:1002
本文介绍了PrintWriter autoflush令人费解的逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public PrintWriter(OutputStream out,boolean autoFlush)


out - An output stream
autoFlush - A boolean; if true, the println, printf, or format methods
will flush the output buffer


public PrintStream(OutputStream out,boolean autoFlush)


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




在这些类之间更改autoflush逻辑的原因是什么?



因为它们总是被认为是相同的,除了编码时刻和autoflush而没有刷新 print()几乎不符合原则最不惊讶,发生了愚蠢的错误:

我创建了一个带有autoflush的PrintWriter;为什么不自动刷新?

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?

推荐答案

我认为答案在于Java的历史。 <$ c中的三重奏 InputStream OutputStream PrintStream $ c> java.io 追溯到Java 1.0。这是在文件编码和字符集内置于语言中的严重支持之前。

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.

引用Javadoc:

PrintStream将功能添加到
另一个输出流,即
能够方便地打印
各种数据值的表示。两个
其他功能
与其他输出流不同,
PrintStream永远不会抛出
IOException;相反,特殊的
情况只是设置一个可以测试的内部标志
通过checkError
方法......

"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..."

总结一下,它可以方便地生成文本输出,嫁接在顶部低级IO。

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

在Java 1.1中, Reader Writer PrintWriter 。那些都支持字符集。虽然 InputStream OutputStream 仍有实际用途(原始数据处理), PrintStream 变得不那么相关了,因为本质上的打印是关于文本的。

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.

Javadoc> PrintWriter 明确声明:


与PrintStream类不同,如果启用了
自动刷新,则仅在一个时间内完成
每当输出换行符
时,调用println()
方法,而不是
。 println()方法
使用平台自己的行
分隔符而不是换行符
字符。

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.

换句话说,PrintWriter只能通过 print *(...) API使用,因为编写换行符等不应该是调用者的责任,处理文件编码和字符集的方式也不是调用者的责任。

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.

我认为 PrintWriter 应该有已经 java.io.Printer 而不是扩展 Writer 。我不知道他们是否扩展到模仿 PrintStream ,或者因为他们坚持维护管道设计习惯用语。

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.

这篇关于PrintWriter autoflush令人费解的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆