C# StreamReader and StreamWriter Explained - Encoding with the classes
(Page 4 of 4 )
In this example I'm going to show you how character encoding is handled with StreamWriter and StreamReader classes. Let's first look at the code example then discuss it.
using System;
using System.IO;
using System.Text;
namespace MyStreams
{
class Class1
{
public static void Main()
{
try
{
FileStream fs = new FileStream("File1.txt",
FileMode.Create, FileAccess.ReadWrite);
using(StreamWriter sw = new StreamWriter(fs))
{
sw.Write("h");
}
using(StreamWriter sw = new StreamWriter("File2.txt",
false,Encoding.UTF8))
{
sw.Write("h");
}
using(StreamWriter sw = new StreamWriter("File3.txt",false,Encoding.Unicode))
{
sw.Write("h");
}
using(StreamWriter sw = new StreamWriter("File4.txt",false,Encoding.BigEndianUnicode))
{
sw.Write("h");
}
// reading the bytes of the 4 files
for(int i = 1; i < 5; i++)
{
string fileName = "File"+i+".txt";
using(fs = new FileStream(fileName,FileMode.OpenOrCreate))
{
Console.WriteLine("nThe file {0} contains the following bytes: n", fileName);
int temp;
while((temp = fs.ReadByte()) != -1)
{
Console.Write(" {0} ", temp);
}
}
}
Console.ReadLine();
}
catch(IOException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Running the above code returns the bytes of every file as illustrated in the following screen shot:

Yes, we have written the letter "h" to the four files, but because StreamWriter can be used to write characters in UTF8, UTF7, Unicode and BigEndianUnicode encoding we got the above bytes written to the underlying FileStream instances. These in turn have written the bytes to the source files. If you are not familiar with Unicode I advise you to read an article about it, because you must know how the characters are encoded. The first statement in the try block creates a FileStream instance which is used by the first StreamWriter instance to write the letter "h" to the file.
As we said before, the default encoding for the StreamWriter is UTF8, but the StreamWriter instance's constructor overload that we have used doesn't write the BOM (Byte Order Mark). The next three using statements create three StreamWriters specifying the encoding used in the constructor. The second statement specifies UTF8 encoding to the constructor, which writes the Byte Order Mark 239 187 191 then the value of the character 'h' 104.
The third using statement specifies Unicode encoding which writes the Byte Order Mark 255 254, then the value of the Unicode 'h' as 104 0. The last using statement specifies BigEndianUnicode encoding which writes the Byte Order Mark 254 255 and the Unicode letter 'h' as 0 104.
Note that we have used the class Encoding of the namespace System.Text in order to specify the three encoding schemes that we need to use. I think that this example illustrate the usefulness of using the StreamWriter to write characters over the FileStream class.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |