Csharp/C Sharp/File Stream/File Stream Encode

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Uses StreamReader and StreamWriter object using different encoding to translate a file from one to another

<source lang="csharp"> /* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794

  • /

// StrmCnvt.cs -- Uses StreamReader and StreamWriter object using different // encoding to translate a file from one to another // // Compile this program with the following command line: // C:>csc StrmCnvt.cs using System; using System.IO; using System.Text; namespace nsStreams {

   public class StrmCnvt
   {
       static public void Main ()
       {
           FileStream istrm;
           FileStream ostrm;
           StreamReader reader;
           StreamWriter writer;
           try
           {

// Open the input file

               istrm = new FileStream ("./StrmRdr.txt", FileMode.Open, FileAccess.Read);

// Link a stream reader to the stream

               reader = new StreamReader (istrm, Encoding.ASCII);
           }
           catch (Exception e)
           {
               Console.WriteLine (e.Message);
               Console.WriteLine ("Cannot open ./StrmRdr.txt");
               return;
           }
           try
           {

// Open the output file

               ostrm = new FileStream ("./StrmRdr.Uni", FileMode.OpenOrCreate, FileAccess.Write);

// Link a stream reader to the stream

               writer = new StreamWriter (ostrm, Encoding.Unicode);
           }
           catch (Exception e)
           {
               Console.WriteLine (e.Message);
               Console.WriteLine ("Cannot open ./StrmRdr.Uni");
               return;
           }
           ostrm.SetLength (0);
           while (reader.Peek () >= 0)
           {
               string str = reader.ReadLine ();
               writer.WriteLine (str);
           }
           reader.Close ();
           istrm.Close ();
           writer.Close ();
           ostrm.Close ();
       }
   }

} //File: StrmRdr.txt /*

                I Hear America Singing

I hear American Mouth-Songs, the varied carols I hear; Those of mechanics -- each one singing his, as it should be,

       blithe and strong;

The carpenter singing his, as he measures his plank or beam, The mason singing his, as he makes ready for work, or leaves

       off work;

The boatman singing what belongs to him in his boat -- the

       deckhand singing on the steamboat deck;

The shoemaker singing as he sits on his bench -- the hatter

       singing as he stands;

The wood-cutter"s song -- the ploughboy"s, on his way in the

       morning, or at the noon intermission, or at sundown;

The delicious singing of the mother -- or of the young wife at

       work -- or of the girl sewing or washing -- Each singing
       what belongs to her, and to none else;

The day what belongs to the day -- At night, the party of young

       fellows, robust, friendly;

Singing, with open mouths, their strong melodious songs.

                            -- Walt Whitman, 1860
  • /
      </source>