This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

byte streams vs character streams

 
aur rashed
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you checked out the Java I/O tutorial? http://download.oracle.com/javase/tutorial/essential/io/streams.html

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic