Comparing two stream writers

In this post I will compare two popular stream writers that C# offers: System.IO.FileStream and System.IO.StreamWriter. Here I only concentrate on stream writers, since one can assume that similar properties apply to the stream readers. Also, in this post I discuss file streaming only. There are several other stream types out there, for example network streams or memory streams. I will not discuss them in this post. Perhaps sometimes later…

I don’t know about you, reader, but whenever I have to read or write from a file I pause and think “So, how does one read from a file? What is the syntax, again?” I then proceed by doing a quick google search to remind myself of the syntax. If I am lucky, I quickly locate an easy example, plug-in my own file name and read/write attributes and carry on without pausing to think about the difference between all these ways one can deal with a file stream. Well, this was all about to change once I decided to educate myself properly, and spread the knowledge along the way.

At the top of the System.IO namespace, one can find two abstract classes: System.IO.Stream and System.IO.TextWriter. It is from these abstract classes that System.IO.FileStream and

System.IO.StreamWriter derive respectively. Both classes support synchronous and asynchronous writing. Also, both classes support buffered streaming. Practically speaking, the main difference between the abstract classes is the type of streams they have been designed to support. FileStream supports a more primitive stream of raw byte data, and to make it useful one needs to convert the textual data to bytes that FileStream can operate on. Note that NetworkStream and MemoryStream too operate on raw byte data. StreamWriter, on the other hand, can be used directly with text-based data, such as strings. Take a look at the example code below. In the main method on line 13 I convert the string value to an array of bytes, which is then passed to an instance of FileStream to write to a file. As a counter example, on line 21 I use an instance of StreamWriter to write the string value to a file, without any conversion. At a high level, this is the only practical difference between FileStream and StreamWriter.


using System.IO;

namespace FunWithStreams
{
 class StreamExample
 {
  static void Main(string[] args)
  {
   long position;
   using (FileStream fs = File.Open("examplefile.txt",FileMode.Append))
   {
    string fileText = "This text will be written to a file.";
    byte[] byteText = Encoding.Default.GetBytes(fileText);
    fs.Write(byteText, 0, byteText.Length);
    position = fs.Position;
   }

   using (StreamWriter sw = new StreamWriter("examplefile.txt", true))
   {
    string fileText = "\r\nThis text will also be written to a file.";
    sw.Write(fileText, position+10);
   }
  }
 }
}

Please note that in the above example I am relying on the compiler to call the Dispose for me to effectively close the streams fs and sw. This is the advantage of using the using(){} syntactic sugar statement.

There are two more, lower level differences worth writing about. These are listed below:

  • FileStream supports random file access (using Seek method), while StreamWriter does not.
  • In the MSDN documentation one can read about the Filestream’s position detection capability. The FileStream object routinely checks on methods that access its cached buffer to assure that the operating system’s handle position is the same as its own cached position. If it detects a change in the handle positions during writing, the contents of the buffer are discarded and an IOException exception is thrown.

I hope you’ve enjoyed this quick post on FileStream and StreamWriter. Please take a look at this article on CodeGuru website written by Richard Grimes for more insight into Streams.

Advertisements
This entry was posted in C# Programming and tagged . Bookmark the permalink.