Binary file I/O - use FileStream Class or BinaryReader/Writer?

I am doing binary byte oriented File I/O. with non-sequential access.

I started using BinaryReader and BinaryWriter Classes. I soon discovered that those classes don't apparently do Seek etc. so I switched to using the FileStream Class. I cannot see much advantage in using BinaryReader/BinaryWriter when I can do everything I need to do with FileStream.

When would it be beneficial to use BinaryReader/BinaryWriter and not the FileStream class

Thanks,

David Harrison



Answer this question

Binary file I/O - use FileStream Class or BinaryReader/Writer?

  • RobAtCDW

    yeah, since most of your access will be just reading and writing large sections of bytes its probably easier to use the stream directly.
  • VB-Man

    Greg, I don't quiet follow your statement : "...ease of reading/Writing data".

    It is already very easy to do using the FileStream class - e.g. it has the set/get Position property and the Seek method for positioning and querying the position in the file.

    BinaryReader/Writer do not "appear" to have that. Unless I am missing something really basic, what additional functionality is wrapped up in the BinaryReader/Writer classes that I cannot access using the FileStream class

    Thanks for any more light you can shed on this.

    - David.


  • Attiks

    Greg, thanks, my data is raw data e.g. audio samples (8 bit unsigned), so they really are just bytes. Seems my choice to use FileStream Class was the right one. I find it somewhat easier to understand.

    Thanks for your help - David (Ottawa, Canada).


  • Pirks

    well one major difference is the ease of reading/writing data.

    The Reader/Writer are nothing more than helpers for the stream you could quite easiy duplicate this functionality on your own as well.

    Greg


  • Imad Mohammed

    If I want to write an integer in binary format to a stream; I have to first change it to bytes, then write it to the stream. The streamwriter has a method available for me that I can simply pass an integer too. If your data is already in the form of bytes, then there is little difference between the two.


  • Binary file I/O - use FileStream Class or BinaryReader/Writer?