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 ?

8
  • 2
    One is suitable for text files, the other for binary files? As you even elude to in your sample code. Commented Apr 27, 2012 at 15:58
  • @Adam Houldsworth utf8 string can contain binary data also. Commented Apr 27, 2012 at 16:01
  • 1
    Yes, but presumably encoded as a UTF-8 string... so in essence would be text. Commented Apr 27, 2012 at 16:02
  • @AdamHouldsworth isnt a utf8 string can hold any info ? Commented Apr 27, 2012 at 16:04
  • @RoyiNamir "utf8 string can contain binary data" - what do you consider "binary data" ? Commented Apr 27, 2012 at 16:08

1 Answer 1

85

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.

Sign up to request clarification or add additional context in comments.

12 Comments

@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 csharpindepth.com/Articles/General/Unicode.aspx
@Nudier: No, neither StreamReader nor BinaryReader derive from Stream. There is absolutely a huge difference between using StreamReader and using BinaryReader.
@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.
@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.
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.