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.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.