Uses StreamReader and StreamWriter object using different encoding to translate a file from one to another : File Stream Encode « File Stream « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » File Stream » File Stream EncodeScreenshots 
Uses StreamReader and StreamWriter object using different encoding to translate a file from one to another

/*
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

*/

           
       
Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.