为什么System.out / err在Java中实现为Byte Streams?

时间:2023-02-15 17:50:46

I was having a look at this tutorial at Sun on command line I/O. It stated that:

我在Sun的命令行I / O上看了一下这个教程。它表示:

You might expect the Standard Streams to be character streams, but, for historical reasons, they are byte streams. System.out and System.err are defined as PrintStream objects. Although it is technically a byte stream, PrintStream utilizes an internal character stream object to emulate many of the features of character streams.

您可能希望标准流是字符流,但由于历史原因,它们是字节流。 System.out和System.err定义为PrintStream对象。虽然从技术上讲它是字节流,但PrintStream利用内部字符流对象来模拟字符流的许多功能。

Does any one know what "the historical reasons" are?

有谁知道“历史原因”是什么?

3 个解决方案

#1


The "historical reasons" are that character streams did not exist in Java 1.0. Apparently Sun realized that the character translation model was insufficient and the Character oriented Reader/Writer class hierarchy was added in Java 1.1.

“历史原因”是Java 1.0中不存在字符流。显然,Sun意识到字符翻译模型不足,并且在Java 1.1中添加了面向字符的Reader / Writer类层次结构。

But it was too late for System.out and friends.

但是对于System.out和朋友来说已经太晚了。

#2


I would guess that the reason is to be backwards-compatible with POSIX C/C++ standard I/O, in which stdin, stdout, and stderr (the C/C++ equivalent of System.in, System.out, and System.err respectively) are byte streams. In Windows, those streams perform CRLF translation, but in POSIX systems, no such conversions occur, and you can freely read and write non-character binary data. Lots of Unix utilities do this, such as GNU's gzip and tar.

我猜这个原因是与POSIX C / C ++标准I / O向后兼容,其中stdin,stdout和stderr(分别是System.in,System.out和System.err的C / C ++等价物) )是字节流。在Windows中,这些流执行CRLF转换,但在POSIX系统中,不会发生此类转换,您可以*地读取和写入非字符二进制数据。很多Unix实用程序都这样做,比如GNU的gzip和tar。

#3


Remember that characters in Java use a 16 bit Unicode character. The original System.in etc needed to be compatible with the environments which supported Java, which (back then at the Dawn of Time) often didn't support Unicode. That, along with the annoyingly disparate treatment of line ends, meant that byte streams were the only type that had the same semantics no matter what platform.

请记住,Java中的字符使用16位Unicode字符。原始的System.in等需要与支持Java的环境兼容,后者(当时的黎明时分)通常不支持Unicode。这与线端的令人讨厌的不同处理意味着字节流是唯一具有相同语义的类型,无论在什么平台上。

#1


The "historical reasons" are that character streams did not exist in Java 1.0. Apparently Sun realized that the character translation model was insufficient and the Character oriented Reader/Writer class hierarchy was added in Java 1.1.

“历史原因”是Java 1.0中不存在字符流。显然,Sun意识到字符翻译模型不足,并且在Java 1.1中添加了面向字符的Reader / Writer类层次结构。

But it was too late for System.out and friends.

但是对于System.out和朋友来说已经太晚了。

#2


I would guess that the reason is to be backwards-compatible with POSIX C/C++ standard I/O, in which stdin, stdout, and stderr (the C/C++ equivalent of System.in, System.out, and System.err respectively) are byte streams. In Windows, those streams perform CRLF translation, but in POSIX systems, no such conversions occur, and you can freely read and write non-character binary data. Lots of Unix utilities do this, such as GNU's gzip and tar.

我猜这个原因是与POSIX C / C ++标准I / O向后兼容,其中stdin,stdout和stderr(分别是System.in,System.out和System.err的C / C ++等价物) )是字节流。在Windows中,这些流执行CRLF转换,但在POSIX系统中,不会发生此类转换,您可以*地读取和写入非字符二进制数据。很多Unix实用程序都这样做,比如GNU的gzip和tar。

#3


Remember that characters in Java use a 16 bit Unicode character. The original System.in etc needed to be compatible with the environments which supported Java, which (back then at the Dawn of Time) often didn't support Unicode. That, along with the annoyingly disparate treatment of line ends, meant that byte streams were the only type that had the same semantics no matter what platform.

请记住,Java中的字符使用16位Unicode字符。原始的System.in等需要与支持Java的环境兼容,后者(当时的黎明时分)通常不支持Unicode。这与线端的令人讨厌的不同处理意味着字节流是唯一具有相同语义的类型,无论在什么平台上。