47

Both StreamReader and BinaryReader can be used to get data from binary file ( for example )

BinaryReader :

   using (FileStream fs = File.Open(@"c:\1.bin",FileMode.Open))
            {
                    byte[] data = new BinaryReader(fs).ReadBytes((int)fs.Length);
                    Encoding.getstring....
            }

StreamReader :

  using (FileStream fs = File.Open(@"c:\1.bin",FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs,Encoding.UTF8))
                {
                       var myString=sr.ReadToEnd();
                }
            }

What is the difference and when should I use which ?

BeRecursive
  • 6,286
  • 1
  • 24
  • 41
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

1 Answers1

84

Both StreamReader and BinaryReader can be used to get data from binary file

Well, StreamReader can be used to get text data from a binary representation of text.

BinaryReader can be used to get arbitrary binary data. If some of that binary data happens to be a representation of text, that's fine - but it doesn't have to be.

Bottom line:

  • If the entirety of your data is a straightforward binary encoding of text data, use StreamReader.
  • If you've fundamentally got binary data which may happen to have some portions in text, use BinaryReader

So for example, you wouldn't try to read a JPEG file with StreamReader.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • but a utf8 string can contains inside it even binary data.. so... ? – Royi Namir Apr 27 '12 at 16:01
  • isnt a utf8 string can hold any info ? – Royi Namir Apr 27 '12 at 16:04
  • 4
    @RoyiNamir: No, a string is text. There's not even such a thing as a "UTF-8 string" - there's a UTF-8 *binary representation* of a string... A string holds text data - it's a sequence of characters. You then convert that to a binary representation using an encoding, such as UTF-8. Please read http://csharpindepth.com/Articles/General/Unicode.aspx – Jon Skeet Apr 27 '12 at 16:04
  • as you may know they all inherits from the base class stream, so there is no difference to use one of those. – Nudier Mena Apr 27 '12 at 16:05
  • @jon what im saying is that there isnt a binary charcter which i cant save it in string... so i can get the bin content to string via StreamReader and BinaryReader – Royi Namir Apr 27 '12 at 16:06
  • 1
    @Nudier: No, neither StreamReader nor BinaryReader derive from Stream. There is *absolutely* a huge difference between using StreamReader and using BinaryReader. – Jon Skeet Apr 27 '12 at 16:06
  • 6
    @RoyiNamir: "Binary character" is a contradiction in terms. It's like talking about a "floating point integer". You **must not** read arbitrary binary data (e.g. a JPEG file) as text. You *will* lose information. – Jon Skeet Apr 27 '12 at 16:07
  • @jon ok ... one final thing : ive just read a jpeg file using binary reader -byte array size was 740. than i read via streamreader - to a string - string .length was still 740.... so...? – Royi Namir Apr 27 '12 at 16:10
  • 1
    @RoyiNamir: Are you converting it into text in a naive way in both cases? If so, you're probably losing data in both cases, possibly consistently. Just don't do it - binary data *isn't* text, so don't try to pretend it is. If you really need to represent arbitrary binary data in text, use base64. – Jon Skeet Apr 27 '12 at 16:12
  • @JonSkeet i got my problem. unicode is only for text. i thought it could also represent some ugly charcters . this was the source of my mistake. thanks. – Royi Namir Apr 27 '12 at 16:29
  • 2
    @RoyiNamir: Unicode can represent almost any *character* you care to mention. But the key word here is *character* - not *byte*. Characters are for text. If you haven't got text, you haven't got characters. – Jon Skeet Apr 27 '12 at 16:31
  • @giorgim: Well that's why you can lose data. And fundamentally it's just a bad idea to treat data of one form as another - it encourages you to perform all kinds of inappropriate operations on it. – Jon Skeet Dec 10 '14 at 09:24