46

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

Ojonugwa Jude Ochalifu
  • 23,935
  • 25
  • 104
  • 122
JavaUser
  • 22,494
  • 44
  • 98
  • 127
  • 9
    dear downvoter - please encourage the author to provide better question by leaving a not **why** you decided to downvote (personally I don't see a strong reason) – Andreas Dolk Jun 10 '10 at 11:57
  • 2
    Open MS Word document in notepad. What do you see? Garbage like as when you open an `exe` in notepad? It'll then be binary data :) – BalusC Jun 10 '10 at 11:57
  • 1
    I vote this post because what I see from the post is the confusion between byte stream and character stream. It doesn't make sense to explain the question using notepad. The downvoter seems to me very arrogant . – Paul Sep 06 '11 at 07:22
  • I don't understand why would anyone downvote this question or any question for that matter unless it has been repeated. Thanks a ton for asking this question and thanks @kgiannakakis for answering so nicely. I tried searching everywhere couldn't find a proper answer until I reached here. – Ghazali Jun 04 '20 at 19:11

5 Answers5

57

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.

kgiannakakis
  • 96,871
  • 26
  • 155
  • 191
  • Thanks kgiannakakis, and What about .txt file ? – JavaUser Jun 10 '10 at 11:58
  • 2
    A character stream is appropriate for reading a .txt file. As I said however, you need to know the encoding of the text file. – kgiannakakis Jun 10 '10 at 12:00
  • i am asking about .txt file present in a windows OS – JavaUser Jun 10 '10 at 12:04
  • 2
    @JavaUser it doesn't matter on which OS you have your .txt file, it can be in any character encoding, and in general it is not possible to unambiguously detect the character encoding that's used in a .txt file. The most common encodings are `ISO-8859-1` and `UTF-8`. – Jesper Jun 10 '10 at 12:29
  • Your Word document might be text format if you've used one of the new XML formats :-) – dty Jun 10 '10 at 12:49
13

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.

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.

Ravindra babu
  • 42,401
  • 8
  • 208
  • 194
6

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

Urjit
  • 61
  • 1
  • 1
4

Read this. It tells you about the difference between bytes and characters (as well as loads of other useful stuff)

dty
  • 18,132
  • 6
  • 51
  • 78
0

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.

V.Dev
  • 147
  • 1
  • 11