12.1 Creating a New File

 <  Day Day Up  >  

You want to programmatically create a new file on the local file system.


Technique

One of the first things you have to consider before you start writing the code to write data to a file is the file format. When we say file format, however, we don't mean how the data itself is logically organized within the file but instead the type of data that will eventually be written. A file is either text-based or binary, although the differences between the two are fuzzy because files by nature are always binary. However, text-based file formats write data out according to its string representation. For instance, if you want to write integer values to a text file, the ASCII values of the integers rather than the integers themselves are written. In other words, if you write the integer 42 to a text file, you actually write the ASCII equivalent of 0x34 and 0x32, representing each digit. A binary formatting scheme uses the actual value of 42.

To create new file in preparation for writing text, use the StreamWriter class in conjunction with the File.CreateText method. The CreateText method has a single parameter, which is the path of the file to create:

 
 private void SaveText( string text, string file ) {     StreamWriter sw = null;     try     {         // 1. Creating a new file for text output         sw = File.CreateText( file );     }     catch( Exception e )     {         MessageBox.Show( e.Message );     }     finally     {         if( sw != null )             sw.Close();     } } 

Whenever data is written using the method just mentioned, data is converted to its ASCII equivalent. Whenever you open the file in a simple text editor, the data is readable. The converse is writing binary data. There are a myriad different ways to create a file for binary output. One of these, similar to the method shown earlier, is to call the Create method defined in the File class. This method returns a FileStream object:

 
 private void SaveBinary( byte[] bytes, string file ) {     FileStream fs = null;     try     {         if( File.Exists( file ) )         {             File.Delete(file);         }         //1. Creating a new file for binary output         fs = File.Create( file );         fs.Write( bytes, 0, bytes.Length );     }     catch( Exception e )     {         MessageBox.Show( e.Message );     }     finally     {         if( fs != null )             fs.Close();     } } 

Comments

The concept of text- and binary-based file formats is an important one. Each one has its advantages and disadvantages. In some cases, however, the answer on which format to support is pretty clear cut. Text-based formats have the distinct advantage of human readability. If your application uses a text-based file formats, you have a large assortment of external text-editing tools at your disposal. Sure, you can edit binary files using a hex editor, but the possibility of entering bad data is extremely high in those circumstances. One disadvantage of text that is an advantage for binary files is size . If you recall, the integer 42 actually translates into 2 bytes in a text file, one for each digit. In a binary file, you can write that same data in only 1 byte. When the data is of considerable size, the resultant file size is orders of magnitude bigger in a text-based format than in a binary format.

The StreamWriter and FileStream classes are both specializations of certain .NET base classes. The StreamWriter is a TextWriter object, which makes sense because its purpose is to write textual data to a file. Some of the other derived classes include the HtmlTextWriter , which contains methods to output HTML tags and attributes; the StringWriter , which performs writing on a string object rather than an actual file; and the HttpWriter class, which sends data to a remote client application.

The FileStream is one class in a group of Stream -based classes. Some of these classes are designed to work with binary values in memory, networking, and cryptography. Each one of these classes simply represents a specialized way to write out a sequence of bytes to their applicable destinations.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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