字符流和字节流之间缺乏一对一的关系

发布于 2024-09-03 01:19:43 字数 811 浏览 3 评论 0原文

我希望就类在其层次结构中的组织方式而言,字符流和字节流之间存在一对一的对应关系。

FilterReaderFilterWriter (字符流)对应于 FilterInputStreamFilterOutputStream (字节流)类。

但是我注意到一些变化,因为

  • BufferedInputStream 扩展了 FilterInputStream,但 BufferedReader 没有扩展 FilterReader

  • BufferedOutputStreamPrintStream 都扩展了 FilterOutputStream,但 BufferedWriterPrintWriter 则扩展了 FilterOutputStream不扩展 FilterWriter

  • FilterInputStreamFilterOutputStream 不是抽象类,但 FilterReaderFilterWriter 是。

我不确定我是否过于偏执而无法指出这些差异,但我只是想知道这样的决定背后是否有设计推理。

I expected to have a one-to-one correspondence between the character streams and byte streams in terms of how the classes are organized in their hierarchy.

FilterReader and FilterWriter (character streams) correspond back to FilterInputStream and FilterOutputStream (byte stream) classes.

However I noticed few changes as -

  • BufferedInputStream extends FilterInputStream, but BufferedReader does NOT extend FilterReader.

  • BufferedOutputStream and PrintStream both extend FilterOutputStream, but BufferedWriter and PrintWriter does NOT extend FilterWriter.

  • FilterInputStream and FilterOutputStream are not abstract classes, but FilterReader and FilterWriter are.

I am not sure if I am being too paranoid to point out such differences, but was just curious to know if there was design reasoning behind such decision.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡看悲欢离合 2024-09-10 01:19:43

Input/OutputStream 类已经是 Java 1.0 的一部分,而 Reader/Writer 类仅在 Java 1.1 中添加。然而,所有语言变化似乎都无法解释您提到的设计差异。

The Input/OutputStream classes were already part of Java 1.0, while the Reader/Writer classes were added only in Java 1.1. However, none of the language changes seem to explain the design differences you mentioned.

深海不蓝 2024-09-10 01:19:43

我认为原因很大程度上是历史原因。最初的面向字节的类是在Java早期开发的,并且设计的某些方面不太理想。当 Java 设计者在 JDK 1.1 中引入面向字符的类时,他们借此机会纠正了 Reader/Writer API 中的一些错误。但到那时,许多客户都在使用 Stream API,纠正它们为时已晚。

在您突出显示的情况下,事实证明,没有用于实例化 FilterInputStream 和 FilterOutputStream 类或多态性使用它们的实际用例。

I think that the reason is largely historical. The original byte-oriented classes were developed in the early days of Java, and some aspects of the design were less than ideal. When the Java designers introduced the character-oriented classes in JDK 1.1, they took the opportunity to correct some of the mistakes in the Reader / Writer APIs. But by that time, many customers were using the Stream APIs and it was too late to correct them.

In the cases you highlighted, it turns out that there are no real use-cases for instantiating the FilterInputStream and FilterOutputStream classes or for using them polymorphicly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文