C#
  Home arrow C# arrow 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 18
    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


    C# StreamReader and StreamWriter Explained


    (Page 1 of 4 )

    In the last article we used the FileStream class to create a stream on a file to write/read byte data. We also used its Seek() method to seek in different positions in the stream. Today we are going to talk about the StreamReader and StreamWriter classes and how we can use them. StreamWriter is used to write characters to a file in specific encoding. The StreamReader is used to read characters, in specific encoding, from a file.

    Recall from the last article (C# FileStream Explained) that the FileStream, MemoryStream and BufferedStream classes (as well as the Class NetworkStream) are all derived from the Stream class. But StreamReader and StreamWriter classes don't derive from the Stream Class. The StreamReader is derived from the abstract class TextReader, and StreamWriter is derived from the abstract class TextWriter. Note that both TextReader and TextWriter are derived from System.MarshalByRefObject which in turn is derived from System.Object.

    My point is that StreamReader and StreamWriter don't extend the Stream base class or its descendant stream classes. So how they are used to write to files? Let's talk about that.

    Both StreamWriter and StreamReader are used to write to/read from a stream in a specific encoding. this is illustrated using code in the following sections. Technically speaking, an instance of those classes is passed to a stream-based instance, a FileStream instance for example, that's used internally to write the bytes to the file. Or in another constructor overload you can pass a string path to the source file and a FileStream instance is created internally. Let's begin with an example that writes three lines to a file called aFile.txt

    More C# Articles
    More By Michael Youssef


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

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek