BinaryReader and BinaryWriter
To go beyond raw byte sequences when dealing with I/O data, the BCL designers invented the BinaryReader
and BinaryWriter
classes. Just as StreamReader
and StreamWriter
can be constructed by passing in a Stream
object to a constructor, this abstraction simplifies the use of streams by providing additional methods. For example, where StreamReader
concerns itself with the notion of textual data and provides a ReadLine
method, the BinaryReader
class exposes Read
methods for various primitive multibyte data types.
Let’s look at an example, again using binary files:
using (var fs = File.OpenRead(@"c:\windows\system32\notepad.exe"))using (var br = new BinaryReader(fs)){ br.BaseStream.Position ...
Get C# 5.0 Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.