9.8 Read and Write a Binary File


Problem

You need to write data to a binary file, with strong data typing.

Solution

Create a new System.IO.FileStream object that references the file. To write the file, wrap the FileStream in a System.IO.BinaryWriter and use the overloaded Write method. To read the file, wrap the FileStream in a System.IO.BinaryReader and use the Read method that corresponds to the expected data type.

Discussion

.NET allows you to write or read binary data with any stream by using the BinaryWriter and BinaryReader classes. When writing data with the BinaryWriter , you use the BinaryWriter.Write method. This method is overloaded to support all the common C# .NET data types, including strings, chars, integers, floating point numbers , decimals, and so on. The information will then be encoded as a series of bytes and written to the file. You can configure the encoding used for strings by using an overloaded constructor that accepts a System.Text.Encoding object, as described in recipe 9.7.

You must be particularly fastidious with data types when using binary files. This is because when you retrieve the information, you must use one of the strongly typed Read methods from the BinaryReader . For example, to retrieve decimal data, you use ReadDecimal . To read a string , you use ReadString . (The BinaryWriter always records the length of a string when it writes it to a binary file to prevent any possibility of error.)

The following Console application shows a simple demonstration that writes and then reads a binary file:

 using System; using System.IO; public class BinaryFileTest {     private static void Main() {         // Create a new file and writer.         FileStream fs = new FileStream("test.txt", FileMode.Create);         BinaryWriter w = new BinaryWriter(fs);         // Write a decimal, two strings, and a char.         w.Write(124.23M);         w.Write("Test string");         w.Write("Test string 2");         w.Write('!');         // Make sure all data is written from the internal buffer.         w.Flush();         // Close the file.         w.Close();         fs.Close();         Console.WriteLine("Press Enter to read the information.");         Console.ReadLine();         // Open the file in read-only mode.         fs = new FileStream("test.txt", FileMode.Open);         // Display the raw information in the file.         StreamReader sr = new StreamReader(fs);         Console.WriteLine(sr.ReadToEnd());         Console.WriteLine();         // Read the data and convert it to the appropriate data type.         fs.Position = 0;         BinaryReader br = new BinaryReader(fs);         Console.WriteLine(br.ReadDecimal());         Console.WriteLine(br.ReadString());         Console.WriteLine(br.ReadString());         Console.WriteLine(br.ReadChar());         fs.Close();         Console.ReadLine();     } } 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net