Back to notes -- Keyboard shortcut: 'u'        next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    Writing a text string using three different encodings with StreamWriters.Lecture 10 - slide 13 : 40
Program 1
using System;
using System.IO;
using System.Text;

public class TextWriterProg{

  public static void Main(){
    string str =      "A æ u å æ ø i æ å",                                   
           strEquiv = "A \u00E6 u \u00E5 \u00E6 \u00F8 i \u00E6 \u00E5";
  
    TextWriter                                                               
      tw1 = new StreamWriter(                         // Iso-Latin-1
             new FileStream("f-iso.txt", FileMode.Create),
             Encoding.GetEncoding("iso-8859-1")),                            

      tw2 = new StreamWriter(                         // UTF-8
             new FileStream("f-utf8.txt", FileMode.Create),
             new UTF8Encoding()),                                            

      tw3 = new StreamWriter(                         // UTF-16              
             new FileStream("f-utf16.txt", FileMode.Create),                 
             new UnicodeEncoding());                                         
                                                                             
    tw1.WriteLine(str);     tw1.WriteLine(strEquiv);                         
    tw2.WriteLine(str);     tw2.WriteLine(strEquiv);                         
    tw3.WriteLine(str);     tw3.WriteLine(strEquiv);

    tw1.Close();                                                             
    tw2.Close();
    tw3.Close();
  }

}
 
 
 
 
 
 
 
Two sample equivalent text strings.
 
 
Three TextWriter variables ...
 
 
for ISO Latin 1,
 
 
 
UTF-8,
 
and UTF-16 StreamWriter objects.
The static types of all variables are
TextWriter. The dynamic types are
all StreamWriter. This is typical!
Systematiclaly, write both str and
strEquiv to all three StreamWriters.
 
 
Close all StreamWriters.