File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes byte streams vs character streams Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "byte streams vs character streams" Watch "byte streams vs character streams" New topic

byte streams vs character streams

aur rashed

Joined: Sep 28, 2011
Posts: 2
Hi everyone ,

I am a former C++ programmer who was lately introduced to Java , well, I have a problem understanding the differences between byte streams and character streams , and I got some questions that would help me understand it well , which I didn't find a solution to through the web , so :

1 - When should I use byte and when to use character stream ?

Note : I read about the low-level streams (inputStream) and high-level Stream (writer/reader) , but all I got is that when dealing with text or string I should use character stream , then when should I use low level stream ?

2- why does data appear as not readable in case of using byte streams ? what do the symbols written by byte streams represent ?

3- what is the relation between streams and encoding ?

4- Is there any difference in the speed of writting or reading between character and byte streams ?

I'd be very grateful if anyone answered these question with a little example just for more illustration

last request : I've tried to cover the difference between byte and character streams through these questions as far as I could think of possible differences , so please if you find some points that weren't mentioned in the above question then I'll be thankful if you added it to make it as clear as possible

Thanks in advance
Greg Charles

Joined: Oct 01, 2001
Posts: 2968

Have you checked out the Java I/O tutorial?

The basic issue is that characters in Java are two byte Unicode characters. Often when text is stored into a file (for example), it's not pure Unicode but some different. For example, UTF-8 might map characters in the ASCII range to their single byte equivalents, while turning other characters into two or three bytes codes that may not be the same as the Unicode value. A typical Western encoding might actually just be a subset of Unicode ... mapping characters from Western languages to specific values, but mapping all other characters to question marks. The problem is that when you read an encoded file, you have to use a the same encoding that was used to write it.

Byte streams should really only be used for binary data ... images, sound files, data, etc. However, you can read bytes into a byte array, and use that to construct a String. Most of the time that would work, but you'd risk getting at least partial gibberish if the default encoding used by that constructor didn't match the encoding used on the file or other source of those bytes.
I agree. Here's the link:
subject: byte streams vs character streams
It's not a secret anymore!