Friday, 9 February 2007

FileStream vs File Class

The following lines of code create a file and add some text to the newly created file using the FileStream class.
string origString =
"I never saw an author who was aware that there is any "+
"dimensional difference between a fact and a surmise.\n"+
" - Mark Twain";

// create the file, write to it, save it.
FileStream fs = new FileStream("quote.txt", FileMode.Create);
fs.Write(ASCIIEncoding.ASCII.GetBytes(origString),
0, origString.Length);
fs.Close();
Compare this to the simpler code of the File class, which provides a series of static methods.

StreamWriter sw = File.CreateText("quote2.txt");
sw.Write(origString);
sw.Write(addString);
sw.Close();
 

No comments: