Saving a text file using a specific encoding in C# .NET
December 26, 2014 1 Comment
The StreamWriter object constructor lets you indicate the encoding type when writing to a text file. The following method shows how simple it is:
private static void SaveFile(Encoding encoding) { Console.WriteLine("Encoding: {0}", encoding.EncodingName); string filename = string.Concat(@"c:\file-", encoding.EncodingName, ".txt"); StreamWriter streamWriter = new StreamWriter(filename, false, encoding); streamWriter.WriteLine("I am feeling great."); streamWriter.Close(); }
We saw in this post how to get hold of a specific code page. We also saw that if you only use characters in the ASCII range, i.e. positions 0-127 then most encoding types will handle the string in a uniform way.
Call the above method like this:
SaveFile(Encoding.UTF7); SaveFile(Encoding.UTF8); SaveFile(Encoding.Unicode); SaveFile(Encoding.UTF32);
So we’ll have 4 files at the end each named after the encoding type. Depending on the supported code pages on your PC Notepad may or may not be able to handle the encoding types. Notepad should not have any problem with UTF8 and UTF16. The UTF7 file will probably look OK, whereas UTF32 will most likely look strange. In my case the UTF32 file content looked like this:
I a m f e e l i n g g r e a t .
…i.e. with some bonus white-space in between the characters. Notepad was not able to correctly read UTF32.
The default encoding type is UTF-16 which will suffice in most situations. If you’re unsure then select this code page.
Providing an encoding type which cannot handle certain characters will result in replacement characters to be shown. If we change the string to be saved to “öåä I am feeling great.” and call the SaveFile method like
SaveFile(Encoding.ASCII);
…then you’ll see the following content in Notepad:
??? I am feeling great. ASCII could not handle the Swedish characters öåä and replaced them with question marks.
Read all posts dedicated to file I/O here.
Reblogged this on Bits and Pieces of Code.