Tuesday, September 7th, 2010

Attaching a streamwriter object to a stream

 
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
 
 
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
 
 
// StrmWrtr.cs -- Demonstrates attaching a StreamWriter object to a stream
//
//Compile this program with the following command line:
//    C:>csc StrmWrtr.cs
using System;
using System.IO;
 
 
namespace nsStreams
{
    public class StrmWrtr
    {
        static public void Main (string [] args)
        {
            if (args.Length == 0)
            {
Console.WriteLine ("Please enter a file name");
return;
            }
            FileStream strm;
            StreamWriter writer;
            try
            {
// Create the stream
strm = new FileStream (args[0], FileMode.OpenOrCreate, FileAccess.Write);
// Link a stream reader to the stream
writer = new StreamWriter (strm);
            }
            catch (Exception e)
            {
Console.WriteLine (e.Message);
Console.WriteLine ("Cannot open " + args[0]);
return;
            }
            strm.SetLength (0);
            while (true)
            {
 string str = Console.ReadLine ();
 if (str.Length == 0)
     break;
 writer.WriteLine (str);
            }
            writer.Close ();
            strm.Close ();
        }
    }
}
Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

Readers Comments (0)




Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.


Latest Additions

Popular User Submitted Links

1


Hosted & Optimized JS Libraries – Cached Commons

1


Multi-File Uploader & Resizer – Agile Uploader

1


Open source online notebook-to-do-list manager

1


3 tier app design in silverlight using azure & RIA services

1


Polymaps – A JS Library For Image & Vector-Tiled Maps

eXTReMe Tracker