简体   繁体   English

Java中的字符I / O流与字节I / O流

[英]character I/O stream vs Byte I/O stream in Java

Does Character stream concrete subclasses classes that inherit from abstract class Reader and abstract class Writer have their own implementations of I/O without relying on concrete subclasses of abstract class InputStream and abstract class OutputStream , which are byte based I/O? abstract class Readerabstract class Writer继承的Character流具体子类类是否具有自己的I / O实现,而不依赖于基于字节的I / O abstract class InputStreamabstract class OutputStream具体子abstract class InputStream

I understand that character stream classes take care of character-encoding formats as additional responsibility, But, 我知道字符流类将字符编码格式作为额外的责任,但是,

Why do we have separate hierarchy given under abstract class Reader and abstract class Writer in addition to byte stream class hierarchy under abstract class InputStream and abstract class OutputStream ? 为什么除了在abstract class InputStreamabstract class OutputStream下的字节流类层次结构之外,在abstract class Readerabstract class Writer下又给出了单独的层次结构?

It was a huge language design decision at that time, that in java there is Unicode text, being able to combine all scripts: 当时,这是一个巨大的语言设计决策,因为Java中有Unicode文本,能够合并所有脚本:

  • Binary data: byte[] , InputStream/OutputStream . 二进制数据: byte[]InputStream/OutputStream
  • Text: String/char (16-bit UTF-16), Reader/Writer . 文本: String/char (16位UTF-16), Reader/Writer

The step from binary data to text and inverse, always involves a conversion needing the encoding of the bytes. 从二进制数据到文本和逆数据的步骤始终涉及需要对字节进行编码转换

As Java also uses wrapping child classes like BufferedReader, StringReader, base classes Reader/Writer where sensible. 由于Java也使用包装子类,例如BufferedReader,StringReader,明智的基类Reader / Writer。 Java chose this, and: Java选择了这个,并且:

In this way you can have a text processing method with a Reader parameter. 这样,您便可以使用带有Reader参数的文本处理方法。

In contrast to C java thus has a separate byte type, and a 2-byte char type, holding UTF-16. 与C相比,Java具有一个单独的byte类型和一个2字节的char类型,它们保存UTF-16。 In java one should almost never try to hold binary data in String, or cast byte to char. 在Java中,几乎应该永远不要尝试将二进制数据保存在String中或将字节转换为char。

Does Character stream concrete subclasses classes that inherit from abstract class Reader and abstract class Writer have their own implementations of I/O without relying on concrete subclasses of abstract class InputStream and abstract class OutputStream, which are byte based I/O? 从抽象类Reader和抽象类Writer继承的Character流具体子类类是否具有自己的I / O实现,而不依赖于抽象类InputStream和抽象类OutputStream的具体子类(它们是基于字节的I / O)?

No. 没有。

I understand that character stream classes take care of character-encoding formats as additional responsibility 我了解字符流类将字符编码格式作为额外的责任

Good. 好。

But, why do we have separate hierarchy given under abstract class Reader and abstract class Writer in addition to byte stream class hierarchy under abstract class InputStream and abstract class OutputStream? 但是,除了在抽象类InputStream和抽象类OutputStream下的字节流类层次结构之外,为什么还要在抽象类Reader和抽象类Writer下分别给出层次结构?

For the purpose you have just described. 出于您刚才描述的目的。 They are essentially a family of filters that redefine the I/O APIs in terms of chars rather than bytes. 它们本质上是一系列过滤器,它们根据字符而不是字节来重新定义I / O API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM