简体   繁体   中英

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?

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.

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.

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

eg 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.

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

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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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