Special Edition Using ASP.Net

   

FileStream , BinaryReader , and BinaryWriter

The FileStream class exposes a stream around a file, supporting both synchronous and asynchronous methods . FileStream also is used to implement the standard in, standard out, and standard error streams. The File class is a utility class with static methods. Its primary use is to enable you to create FileStream objects that are based on file paths and the standard input, standard output, and standard error devices. File is used with FileStream .

The FileStream class can open a file in one of two modes: either synchronously or asynchronously, with significant performance consequences for both the synchronous methods ( Read and Write ) and the asynchronous methods ( BeginRead and BeginWrite ). The synchronous mode is faster than the asynchronous. FileStream defaults to opening files synchronously.

The MemoryStream class creates a stream from a byte array and functions similarly to a FileStream . For example, FileStream objects use the Seek method to support random access to files. The Seek method allows the read/write position to be moved to any position within the file. This is done with two parameters: a byte offset and a reference point. The byte offset is relative to the reference point, which can be the beginning, current position, or end of the underlying file. These reference points are represented by the three properties of the SeekOrigin class. (Disk files always support random access. Note that at the time of construction, the CanSeek property is set to true or false depending on the underlying file type. Specifically, if the underlying file type is FILE_TYPE_DISK, as defined in winbase.h, then the CanSeek property is true, and otherwise , false.)

The BinaryReader class reads strings and primitive data types. The BinaryWriter class writes primitive types in binary from a stream and supports writing strings in a particular encoding. A subclass can override the methods of this class to give unique character encodings.

The example for this section enables users to enter data, and then saves it as binary data to disk. Figure 5.11 shows the example while it's running, and Listing 5.12 shows the code.

Figure 5.11. The BinaryReader and BinaryWriter classes combine to handle binary data.

Listing 5.12 Code Handling Binary Data

Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Try Dim fs As New FileStream(Request.MapPath("BinaryData\MiscData.dat"),_ FileMode.Create) Dim writer As New BinaryWriter(fs) writer.Write(TextBox1.Text.ToInt32()) writer.Write(TextBox2.Text.ToInt32()) writer.Write(TextBox3.Text.ToInt32()) writer.Write(TextBox4.Text.ToDouble()) writer.Write(TextBox5.Text) writer.Close() Label1.Text = "File Saved!" Catch ex As Exception Label1.Text = ex.Message End Try End Sub Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Try Dim fs As New FileStream(Request.MapPath("BinaryData\MiscData.dat"),_ FileMode.Open) Dim reader As New BinaryReader(fs) Label3.Text = Convert.ToString(reader.ReadInt32()) Label4.Text = Convert.ToString(reader.ReadInt32()) Label5.Text = Convert.ToString(reader.ReadInt32()) Label6.Text = Convert.ToString(reader.ReadDouble()) Label7.Text = Convert.ToString(reader.ReadString()) reader.Close() Label2.Text = "File Read!" Catch ex As Exception Label2.Text = ex.Message End Try End Sub

   

Категории