C#
  Home arrow C# arrow C# StreamReader and StreamWriter Explained
Iron Speed
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 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    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

    - 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
    - C# FileStream Explained
    - A Look at C# File and FileInfo Classes
    - A Look at C# Directory and DirectoryInfo Cla...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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