Reads an ASCII encoded file and writes the text to another file in wide character format : Ascii String Read Write « 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 » Ascii String Read WriteScreenshots 
Reads an ASCII encoded file and writes the text to another file in wide character format

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//  Convert.cs -- Reads an ASCII encoded file and writes the text to another file
//                in wide character format.
//                Compile this program with the following command line:
//                    C:>csc Convert.cs
//
namespace nsConvert
{
    using System;
    using System.Text;
    using System.IO;
    public class Convert
    {
        static public int Main ()
        {
// First, make sure both the input and output files can be opened
            FileStream ostream;
            FileStream istream;
            try
            {
                istream = new FileStream ("Sample.asc", FileMode.Open, FileAccess.Read);
            }
            catch (Exception)
            {
                Console.WriteLine ("Cannot open Sample.asc for reading");
                return (-1);
            }
            try
            {
                ostream = new FileStream ("Sample.wcs", FileMode.Create, FileAccess.ReadWrite);
            }
            catch (Exception)
            {
                Console.WriteLine ("Cannot open Sample.wcs for writing");
                istream.Close ();
                return (-1);
            }
// Create a stream reader and attach the input stream with ASCII encoding
            StreamReader reader = new StreamReader (istream, new ASCIIEncoding());
            string str = reader.ReadToEnd ();
// Create a stream writer and attach the output stream using Unicode encoding
            StreamWriter writer = new StreamWriter (ostream, new UnicodeEncoding());
// Write the text to the file.
            writer.Write (str);
// Flush the output stream
            writer.Flush ();
// Close the streams
            ostream.Close ();
            istream.Close ();
            return (0);
        }
    }
}

//File: Sample.asc
/*
The quick red fox jumps over the lazy brown dog.
Now is the time for all good men to come to the aid of their Teletype.
Peter Piper picked a peck of peppered pickles.

*/

           
       
Related examples in the same category
1. Demonstrate StringReader and StringWriter  Demonstrate StringReader and StringWriter
2.Manipulating a string read as a line from a file
3.Using String ReaderUsing String Reader
4.An enhanced cipher component that maintains a log fileAn enhanced cipher component that maintains a log file
5.Text ReaderText Reader
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.