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.

转载于:https://www.cnblogs.com/Bruce3555/p/5459009.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值