< Day Day Up > |
TechniqueSection 12.1 explained how to create a new file in preparation for writing text or bytes to it. When you create a new text file, you receive a StreamWriter object. The Write method defined in this object is able to take most .NET data types and write their values to the file. One particular note, which has been mentioned before, is that the data is written according to its ASCII equivalent. If you call the Write method and pass a float data type, each digit is converted to its actual ASCII table value. Because the Write method contains several overloaded versions, each accepting a different .NET data type, you should have no problem sending it data as you see fit: private void SaveText( string text, string file ) { StreamWriter sw = null; try { // Check if file exists if( File.Exists( file ) ) { File.Delete( file ); } // Create a new file for text output sw = File.CreateText( file ); sw.Write( text ); } catch( Exception e ) { MessageBox.Show( e.Message ); } finally { if( sw != null ) sw.Close(); } } If you want to write your data to a binary files:format of, use the BinaryWriter class by creating an instance and passing the FileStream object returned from the Open method of the File class to the constructor. To write to the file, call the Write method from the BinaryWriter object. This method is one of those methods that contain a lot of overloaded versions, and each version allows you to write out a certain data type. For instance, to write an integer, call the Write method passing an integer, but to write a string, call the Write method with a string: private void TestBinaryWrite( string file ) { FileStream fs = null; BinaryWriter bw; try { // open the file fs = File.Open( file, FileMode.Create ); bw = new BinaryWriter( fs ); // write 5 integers for( int i = 0; i < 5; ++i ) { bw.Write(i); } // write 5 strings for( int i = 0; i < 5; ++i ) { bw.Write( "string" + i.ToString() ); } } catch( Exception e ) { MessageBox.Show( e.Message ); } finally { // close the FileStream if( fs != null ) fs.Close(); } return; } CommentsAs you can see from the two different techniques just shown, writing to either a text-based file or in binary really isn't that much different. The differences lie in how that data is finally translated internally within the classes and written to the file. The BinaryWriter actually performs some additional work to ensure data integrity when the data is finally read by the corresponding BinaryReader mentioned in the next section. When a certain data type is written to the file, the BinaryWriter writes out a number of bytes equivalent to the size of the data type. For instance, if you pass an integer to the Write method that is less than 255, you could actually utilize a single byte of data. However, the BinaryWriter writes 4 bytes of data regardless of how small the actual value is. The remaining bytes in this case are all 0s. If size constraints are within the design constraints of your application, then you need to take this fact into account. If a binary file contains string data, how does the BinaryReader know how many bytes to read of that string unless you specifically tell it the amount (which in and of itself would be difficult)? Whenever these situations occur, the data is prefaced with the size of the corresponding data. With strings, the length of the string is written, followed by the string itself. |
< Day Day Up > |