Byte Stream and Character stream

China☆狼群 提交于 2019-12-17 07:07:28

问题


Please explain what Byte streams and Character streams are. What exactly do these mean? Is a Microsoft Word document Byte oriented or Character oriented?

Thanks


回答1:


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.

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). You need to use a byte stream to access it.




回答2:


ByteStreams:

From oracle documentation page about byte streams:

Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream.

When to use:

Byte streams should only be used for the most primitive I/O

When not to use:

You should not use Byte stream to read Character streams

e.g. To read a text file

Character Streams:

From oracle documentation page about character streams:

The Java platform stores character values using Unicode conventions. Character stream I/O automatically translates this internal format to and from the local character set.

All character stream classes are descended from Reader and Writer.

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.

There are two general-purpose byte-to-character "bridge" streams: InputStreamReader and OutputStreamWriter.

When to use:

To read character streams either from Socket or File of characters

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.




回答3:


1.Character oriented are tied to datatype. 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

3.Character oriented streams use character encoding scheme(UNICODE) while byte oriented do not use any encoding scheme

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)




回答5:


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). Character streams are capable to translate implicitly 8-bit data to 16-bit data or vice versa. 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. Character stream I/O automatically translates this internal format to and from the local character set.

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.



来源:https://stackoverflow.com/questions/3013996/byte-stream-and-character-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!