Writing File Stream C#



FileStream provides a Stream for a file, supporting both synchronous and asynchronous read and write operations.
These classes inherit from the abstract base class Stream, which supports reading and writing bytes into a file stream

using System.IO;
using System;

namespace CSharpFilesAndStreams
{
	class WriteToFile 
	{
		static void Main(string[] args) 
		{
			FileStream fout = null;
			try 
			{
				// Open output file.
				fout = new FileStream("test.txt", FileMode.CreateNew);
				
				// Write the alphabet to the file.
				for(char c = 'A'; c <= 'Z'; c++)
				fout.WriteByte((byte) c);
			} 
			catch(IOException exc) 
			{
				Console.WriteLine("I/O Error:\n" + exc.Message);
			} 
			finally 
			{
				if(fout != null) fout.Close();
			}
		}
	}
}	

0 Comment's

Comment Form

Submit Comment