Input/Output 14: BinaryWriter
Written by:
, 2009-04-17 07:19:17
The Basics
The BinaryWriter class is used to write binary data to a stream. You can construct a BinaryWriter object by passing a Stream object; you can�t pass a file path to create a BinaryWriter object. In addition, there is also a parameterless constructor. The BinaryWriter class� most important method is Write. This method has 18 overloads that guarantee that you can write any type of data to a BinaryWriter object. For example, the following code creates a BinaryWriter object that is linked to a new file called MyBinary.bin, and writes a byte and a string:
BinaryWriter bw = new BinaryWriter(File.Create("
C:/MyBinary.bin"));
Byte b = 17;
string s = "August";
bw.Write(b);
bw.Write(s);
bw.Close();