• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

byte streams vs character streams

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic