Binary Input and Output with the FileStream Class

   


Binary Input and Output with the FileStream Class

FileStream is a commonly used and versatile binary stream class that can be used to read and write individual bytes and blocks of bytes. Because characters are merely encoded bytes, the FileStream class can also be used to stream characters. However, it does not contain the same convenient methods targeted at character streams as do the StreamReader and StreamWriter classes.

The FileStream class can be used to create new files as well as open existing files.

Table 22.5 lists a few of FileStream's class members.

Table 22.5. A Few Selected FileStream Class Members
FileStream class member Description
Close Closes the stream and releases any associated resources
ReadByte Reads one byte from the stream
WriteByte Writes one byte to the stream
Length Returns the length (in bytes) of the stream

Note

graphics/common.gif

Please consult the .NET Framework documentation to view the rest of the FileStream class members.


The WriteBytes method in lines 6 30 of Listing 22.3 demonstrates how the FileStream class can be used to create a file and write ten numbers of type byte to this file. The ReadBytes method in lines 32 65 uses FileStream to read the contents from this new file.

Listing 22.3 ByteWritingReading.cs
01: using System; 02: using System.IO; 03: 04: class TestBytesInOut 05: { 06:     public static void WriteBytes() 07:     { 08:         FileStream outStream = null; 09: 10:         try 11:         { 12:             FileInfo bytesFile = new FileInfo (@"C:\MyTestFiles\numbers1.dat"); 13:             outStream = bytesFile.OpenWrite(); 14: 15:             for (byte i = 0; i < 10; i++) 16:             { 17:                 outStream.WriteByte(i); 18:             } 19:         } 20: 21:         catch (IOException exObj) 22:         { 23:             Console.WriteLine(exObj); 24:         } 25: 26:         finally 27:         { 28:             outStream.Close(); 29:         } 30:     } 31:  32:     public static void ReadBytes() 33:     { 34:         int totalSum = 0; 35:         int temp = 0; 36:         FileStream inStream = null; 37: 38:         try 39:         { 40:             FileInfo numberFile = new FileInfo (@"C:\MyTestFiles\numbers1.dat"); 41:             inStream = numberFile.OpenRead(); 42: 43:             Console.WriteLine("Length: " + inStream.Length); 44:             Console.WriteLine("List of numbers in file:"); 45: 46:             for (int i = 0; i < inStream.Length; i++) 47:             { 48:                 temp = inStream.ReadByte(); 49:                 Console.WriteLine(temp); 50:                 totalSum += temp; 51:             } 52: 53:             Console.WriteLine("\nTotal sum of numbers in file: { 0} ", totalSum); 54:         } 55: 56:         catch (IOException exObj) 57:         { 58:             Console.WriteLine(exObj); 59:         } 60:  61:         finally 62:         { 63:             inStream.Close(); 64:         } 65:     } 66: } 67: 68: class Tester 69: { 70:     public static void Main() 71:     { 72:         TestBytesInOut.WriteBytes(); 73:         TestBytesInOut.ReadBytes(); 74:     } 75: } Length: 10 List of numbers in file: 0 1 2 3 4 5 6 7 8 9 Total sum of numbers in file: 45 

You can use FileStream's own constructor to create a new FileStream object, or you can use one of FileInfo's methods. The latter approach is applied in Listing 22.3, which uses FileInfo's OpenWrite (line 13) and OpenRead (line 41) methods to return a binary output stream and a binary input stream, respectively, both of type FileStream.

Lines 12 and 13 create a new file called C:\MyTestFiles\numbers1.dat that allows the outStream object of type FileStream to write to this file. outStream's method WriteByte takes one argument of type byte and writes it to the new file. In our case, the first ten numbers (starting with zero) are written to the file in lines 15 18.

Notice that C:\MyTestFiles\numbers1.dat is a binary file not a text file, so it's contents cannot be properly viewed by a text editor, such as Notepad. Instead, we can use a binary input stream of type FileStream as returned by FileInfo's OpenRead method in line 41.

We use FileStream's Length property in line 46 to determine the number of times the for loop should be repeated. This makes sense because:

  • Length returns the number of bytes in inStream

  • The loop will be repeated Length times

  • Each loop reads one byte in line 48, starting from the first byte in the file

Thereby we ensure that every single byte of numbers1.dat is assigned to temp in line 48 and written to the console in line 49.

We can also involve the data in numbers1.dat in numeric calculations. In this case (lines 48 and 50), we simply add temp to totalSum for every loop to calculate the total sum of all numbers kept in the numbers1.dat file; the result is written to the console in line 53.


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

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