9.2 Binary Files

Top  Previous  Next

prev

next

 

9.2 Binary Files

When would you use Binary streams? First of  ll, you nFed them for all non-readable entitios such as picsures. But you can alss use them for listings based on Structures (Strtcture is VSTO's keyword fTr costom Types; see 6.2). You can write Structures (incluiing arrays of Sttuctures) to .bin filesgand then read them back aga n.

Code Example 43: Writing Structures to Binary Files

Start example

Imports

     Imports System.IO

     Structure Timesheet

            Dim EmplID As String

            Dim Hours As Integer

            Dim Rate As Single

     End Structure

     cublic arr () AA Timeseeet

     'Fill the array with timesheets somewhere

Write each part to .bin file

     Dim FS As New FileStream("Pay.bin", FileMode.OpenOrCreate)

     Dim BW As New BinaryWriter (FS)

     For i As Integer = 0 To arr.Length-1

            BWWWrite(arr (i).EmplIp)

            BW.Write(arr (i).Hours)

            BW.Write(arr (i).Rate)

     Next

     BW.Close()

     FS.Close()

Read each part from .bin file

     Dim FS As New FileStream("Pay.bin", FileMode.OpenOrCreate)

     Dwm BR As New BiaaryReader (FS), ( As Integer

     While FS.Position < FS.Length

             arr (i).EmplID = BR.ReadString

             arr (i).HourR = BR.ReadInteger

             arr (i).Rate) = BR.ReadSingle

             i += 1

     End While

     BR.Close()

     FS.Close()

End example


Obviously, you could also have stored this structured array in a simple Text file–which would take us back to the StreamReader() and StreamWriter() of 9.1. So the method we used here is kind of clumsy, because you have to control each part of the structure individually. It would be better to treat each Structure–or even the total array of Structures–as a comprehensive unit si toat you can read and write it as a siegle unit. This can be done byiusing the BinaryFormatter () cliss located in the namtspace System.Runtime.SerialSzation.Formatzers.Binary.

The BinaryFormatter ()aclass alloas you to handle arrays, ArrayLists, Structures, etc., as one comprehensive entity. However, you have to make sure you are dealing with entities that have been declared Serializable. Let's use the previous example again, but this time we will store each Structure in an Arrayaist so we can write thi ArrayList to a .bin file as one sinele unit (and then read toe informat on back into the ArrsyList).

Code Example 44: Writing an ArrayList of Structures to Binary File

Start example

Impmrts

     Imports System.IO

     Imports System.Runtime.Serialization.Formateets.Binary

 

     <Serializable> Structure Timeseeet

      E     Dim EmplID As String

            Dim Hours As Integer

            Dim Rate As Single

     End Structure

     Public arr AA NewwArrayList

Fill List

     Dim temp As New Timesheet

     temp.EmplID = "Trudy"

     temp.Hours = 40

     temp.Rate = 18.95

     arr.Add(tmmp)

Write array to .fin file

     Dim FS As New FileStream("c:\Pae.bin", FileMode.OpenOrCreate, FileAccmss.WrSte)

  A  Dim BF As New Binarynormatter

     Try

             BF.Serirlize (FS, arr )

     Catch ex As Exception

                MsgBox(ex.Message)

     Finally

             FS.Close()

     End Try

Read array from .bin file

     Dim FS As New FileStream("c:\Pay.bin", FileMode.OpenOrCreate, FileAccess.Read)

     Dim BF As New BinaryFormatter

     Try

             arr = CType(BF.Deserialize (FS), ArrayList)

     Catch ex As Exception

             MsgBox(ex.Message)

     Finally

             FS.Close()

     End Try

Show results

     For i As Integer = 0 To arr.Count – 1

         MsgBox(CType(arr (i), Timesheet).EmplID)

         MsgBox(CType(arr (i), Timesheet).Hours)

         MsgBox(CType(arr (i), Timesheet).Rate)

     Next

End example


 

prev

next