Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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 ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

byte streams vs character streams

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
Posts: 3036
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?

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.
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic