简体   繁体   English

字节流和字符流

[英]Byte Stream and Character stream

Please explain what Byte streams and Character streams are. 请解释什么是字节流和字符流。 What exactly do these mean? 这些到底是什么意思? Is a Microsoft Word document Byte oriented or Character oriented? Microsoft Word文档是面向字节还是面向字符?

Thanks 谢谢

A stream is a way of sequentially accessing a file. 流是顺序访问文件的一种方式。 A byte stream access the file byte by byte. 字节流逐字节访问文件。 A byte stream is suitable for any kind of file, however not quite appropriate for text files. 字节流适用于任何类型的文件,但不适用于文本文件。 For example, if the file is using a unicode encoding and a character is represented with two bytes, the byte stream will treat these separately and you will need to do the conversion yourself. 例如,如果文件使用unicode编码,并且一个字符用两个字节表示,则字节流将分别处理这些字节,您需要自己进行转换。

A character stream will read a file character by character. 字符流将逐字符读取文件。 A character stream needs to be given the file's encoding in order to work properly. 字符流需要使用文件的编码才能正常工作。

Although a Microsoft Word Document contains text, it can't be accessed with a character stream (it isn't a text file). 尽管Microsoft Word文档包含文本,但是无法使用字符流(它不是文本文件)进行访问。 You need to use a byte stream to access it. 您需要使用字节流来访问它。

ByteStreams : ByteStreams

From oracle documentation page about byte streams : 从oracle文档页面中有关字节流的信息

Programs use byte streams to perform input and output of 8-bit bytes. 程序使用字节流执行8位字节的输入和输出。 All byte stream classes are descended from InputStream and OutputStream . 所有字节流类都从InputStreamOutputStream继承。

在此处输入图片说明

When to use: 何时使用:

Byte streams should only be used for the most primitive I/O 字节流仅应用于最原始的I / O

When not to use: 何时不使用:

You should not use Byte stream to read Character streams 您不应使用字节流读取字符流

eg To read a text file 例如读取文本文件

Character Streams: 字符流:

From oracle documentation page about character streams : 从oracle文档页面中有关字符流的信息

The Java platform stores character values using Unicode conventions. Java平台使用Unicode约定存储字符值。 Character stream I/O automatically translates this internal format to and from the local character set. 字符流I / O自动将此内部格式与本地字符集转换。

All character stream classes are descended from Reader and Writer . 所有字符流类均来自ReaderWriter

Character streams are often "wrappers" for byte streams. 字符流通常是字节流的“包装器”。 The character stream uses the byte stream to perform the physical I/O, while the character stream handles translation between characters and bytes. 字符流使用字节流执行物理I / O,而字符流则处理字符和字节之间的转换。

There are two general-purpose byte-to-character "bridge" streams: InputStreamReader and OutputStreamWriter . 有两个通用的字节到字符“桥”流: InputStreamReaderOutputStreamWriter

When to use: 何时使用:

To read character streams either from Socket or File of characters Socket或字符File中读取字符流

In Summary : 总结

Byte stream reads and write a byte at a time. 字节流一次读取和写入一个字节。 We must avoid the usage of byte stream while dealing with more sophisticated data. 在处理更复杂的数据时,我们必须避免使用字节流。

Character Stream and other available streams should be used to handle sophisticated data. 字符流和其他可用流应用于处理复杂的数据。

1.Character oriented are tied to datatype. 1.面向字符与数据类型相关。 Only string type or character type can be read through it while byte oriented are not tied to any datatype, data of any datatype can be read(except string) just you have to specify it. 只能读取字符串类型或字符类型,而面向字节的数据不与任何数据类型绑定,可以读取任何数据类型的数据(字符串除外),只需指定即可。

2.Character oriented reads character by character while byte oriented reads byte by byte 2.面向字符的读取字符,而面向字节的读取字节

3.Character oriented streams use character encoding scheme(UNICODE) while byte oriented do not use any encoding scheme 3,面向字符的流使用字符编码方案(UNICODE),而面向字节的流不使用任何编码方案

4.Character oriented streams are also known as reader and writer streams Byte oriented streams are known as data streams-Data input stream and Data output stream 4.面向字符的流也称为读取器和写入器流面向字节的流被称为数据流-数据输入流和数据输出流

Read this . 阅读 It tells you about the difference between bytes and characters (as well as loads of other useful stuff) 它告诉您字节和字符之间的区别(以及其他有用内容的负载)

A character stream will read a file character by character. 字符流将逐字符读取文件。 The character streams are capable to read 16-bit characters (byte streams read 8-bit characters). 字符流可以读取16位字符(字节流可以读取8位字符)。 Character streams are capable to translate implicitly 8-bit data to 16-bit data or vice versa. 字符流能够将8位数据隐式转换为16位数据,反之亦然。 Character stream can support all types of character sets ASCII, Unicode, UTF-8, UTF-16 etc.But byte stream is suitable only for ASCII character set.The Java platform stores character values using Unicode conventions. 字符流可以支持所有类型的字符集ASCII,Unicode,UTF-8,UTF-16等,但是字节流仅适用于ASCII字符集.Java平台使用Unicode约定存储字符值。 Character stream I/O automatically translates this internal format to and from the local character set. 字符流I / O自动将此内部格式与本地字符集转换。

Unless you are working with binary data, such as image and sound files, you should use readers and writers to read and write information with character streams. 除非使用图像和声音文件等二进制数据,否则应使用读取器和写入器通过字符流读取和写入信息。

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

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