BinaryReader Class and BinaryWriter Class

Overview

  • Used to read and write binary data from streams.
  • Call the correct Read methods when using a BinaryReader.

Good practice

  • Remember to call Close, since it has a stream behind it.

Examples

Read and Write Binary

FileStream a = File.Create(@"C:\Public\BinaryTest.dat");
 
BinaryWriter b = new BinaryWriter(a);
b.Write(new byte[] { 47, 50 });
b.Write("Hello World");
b.Write(42);
b.Close();
 
a = File.OpenRead(@"C:\Public\BinaryTest.dat");
 
BinaryReader c = new BinaryReader(a);
c.ReadBytes(2);
Console.WriteLine(c.ReadString());
Console.WriteLine(c.ReadInt32());
c.Close();
 
learning/exam70-536/binaryreader.txt · Last modified: 2008/08/27 13:50 by david
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki