Stream Writer With Parameter Constructor C# with Example



Initializes a new instance of the StreamWriter class for the specified stream by using the specified encoding and buffer size
StreamWriter StreamWriter(String, Boolean) Initializes a new instance of the StreamWriter class for the specified file by using the default encoding and buffer.

using System.IO;
using System;

namespace CSharpFilesAndStreams
{
	class KtoD 
	{
		static void Main() 
		{
			string str;
			StreamWriter fstr_out = null;
			try {
				// Open the file, wrapped in a StreamWriter.
				fstr_out = new StreamWriter("test.txt");
				Console.WriteLine("Enter text ('stop' to quit).");
				do {
					Console.Write(": ");
					str = Console.ReadLine();
					if(str != "stop") {
					str = str + "\r\n"; // add newline
					fstr_out.Write(str);
					}
				} while(str != "stop");
			} 
			catch(IOException exc) {
				Console.WriteLine("I/O Error:\n" + exc.Message);
			} 
			finally {
				if(fstr_out != null) fstr_out.Close();
			}
		}
	}
}	

0 Comment's

Comment Form

Submit Comment