byte stream and character stream

http://stackoverflow.com/questions/3013996/byte-stream-and-character-stream

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.

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

 

3.

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.

enter image description here

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.

posted @ 2016-05-04 16:50  蒋孝愚  阅读(394)  评论(0编辑  收藏  举报