SunQuest
 
       C#
  Home arrow C# arrow Page 4 - C# StreamReader and StreamWriter Explained
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# StreamReader and StreamWriter Explained
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2007-02-06

    Table of Contents:
  • C# StreamReader and StreamWriter Explained
  • Using StreamWriter to write to a file
  • Step-by-step through the code example
  • Encoding with the classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    C# StreamReader and StreamWriter Explained - Encoding with the classes


    (Page 4 of 4 )

    In this example I'm going to show you how character encoding is handled with StreamWriter and StreamReader classes. Let's first look at the code example then discuss it.

    using System;
    using System.IO;
    using System.Text;

    namespace MyStreams
    {
      class Class1
      {
        public static void Main()
        {
          try
          {
            FileStream fs = new FileStream("File1.txt",
    FileMode.Create, FileAccess.ReadWrite);
            using(StreamWriter sw = new StreamWriter(fs))
            {
              sw.Write("h");
            }
            using(StreamWriter sw = new StreamWriter("File2.txt",
    false,Encoding.UTF8))
            {
              sw.Write("h");
            }
            using(StreamWriter sw = new StreamWriter("File3.txt",false,Encoding.Unicode))
            {
              sw.Write("h");
            }
            using(StreamWriter sw = new StreamWriter("File4.txt",false,Encoding.BigEndianUnicode))
            {
              sw.Write("h");
            }

            // reading the bytes of the 4 files
            for(int i = 1; i < 5; i++)
            {
              string fileName = "File"+i+".txt";
              using(fs = new FileStream(fileName,FileMode.OpenOrCreate))
              {
                Console.WriteLine("nThe file {0} contains the following bytes: n", fileName);
                int temp;
                while((temp = fs.ReadByte()) != -1)
                {
                  Console.Write(" {0} ", temp);
                }
              }
            }
            Console.ReadLine();
          }
          catch(IOException ex)
          {
            Console.WriteLine(ex.Message);
          }

        }
      }
    }

    Running the above code returns the bytes of every file as illustrated in the following screen shot:

    Yes, we have written the letter "h" to the four files, but because StreamWriter can be used to write characters in UTF8, UTF7, Unicode and BigEndianUnicode encoding we got the above bytes written to the underlying FileStream instances. These in turn have written the bytes to the source files. If you are not familiar with Unicode I advise you to read an article about it, because you must know how the characters are encoded. The first statement in the try block creates a FileStream instance which is used by the first StreamWriter instance to write the letter "h" to the file.

    As we said before, the default encoding for the StreamWriter is UTF8, but the StreamWriter instance's constructor overload that we have used doesn't write the BOM (Byte Order Mark). The next three using statements create three StreamWriters specifying the encoding used in the constructor. The second statement specifies UTF8 encoding to the constructor, which writes the Byte Order Mark 239 187 191 then the value of the character 'h' 104.

    The third using statement specifies Unicode encoding which writes the Byte Order Mark 255 254, then the value of the Unicode 'h' as 104 0. The last using statement specifies BigEndianUnicode encoding which writes the Byte Order Mark 254 255 and the Unicode letter 'h' as 0 104.

    Note that we have used the class Encoding of the namespace System.Text in order to specify the three encoding schemes that we need to use. I think that this example illustrate the usefulness of using the StreamWriter to write characters over the FileStream class.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this simple article you are going to learn how to write to and read from a file...
     

    C# ARTICLES

    - Color Transformation in C# GDI+ Programming
    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway