【问题标题】:Is System.out buffered or unbuffered?System.out 是缓冲的还是无缓冲的?
【发布时间】:2014-06-25 06:15:37
【问题描述】:

System.out 是缓冲的还是非缓冲的?

我读到这是InputStream 的对象,PrinterStreamSystem.out 引用的对象类型。

而且它们都是无缓冲的,所以为什么println() 刷新无缓冲...是否可以刷新无缓冲并且我已经阅读它们是立即写入的。

【问题讨论】:

标签: java


【解决方案1】:

System.out 是“标准”输出。在大多数operating systems 上,终端io 被缓冲,并且支持分页。

来自 Javadoc,

“标准”输出流。此流已经打开并准备好接受输出数据。通常,此流对应于主机环境或用户指定的显示输出或另一个输出目的地。

【讨论】:

  • 好的,没错 System.out 是缓冲的...但我也想知道缓冲输入流是什么意思?
  • 考虑一个管道。一个进程写入,一个进程读取。但是我在哪里提到了缓冲输入流?如果您正在读取标准输入(或另一个进程的输出),则必须有一个输入流可供读取。
【解决方案2】:

来自http://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html

可选地,可以创建PrintStream 以便自动刷新;这意味着在写入字节数组、调用 println 方法之一或写入换行符或字节 ('\n') 后,将自动调用 flush 方法。

【讨论】:

    【解决方案3】:

    System.out.println 将传递的参数打印到通常是标准输出的 System.out 中。

    System – is a final class and cannot be inherited. As per javadoc, “…Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array…”
    out – is a static member field of System class and is of type PrintStream. Its access specifiers are public final. This gets instantiated during startup and gets mapped with standard output console of the host. This stream is open by itself immediately after its instantiation and ready to accept data.
    println – println prints the argument passed to the standard console and a newline. There are multiple println methods with different arguments (overloading). Every println makes a call to print method and adds a newline. print calls write() and the story goes on like that.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-14
      • 2010-10-13
      • 2011-04-13
      • 2020-11-22
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多