Reading File Stream C# with Example



Reading File Stream C# with Example

 using System.IO;
using System;

namespace CSharpFilesAndStreams
{
	class ShowFile 
	{
		static void Main(string[] args) 
		{
			int i;
			FileStream fin;
			if(args.Length != 1) 
			{
				Console.WriteLine("Specify Filename which you want to Open:");
				return;
			}
			try 
			{
				fin = new FileStream(args[0], FileMode.Open);
			} 
			catch(IOException exc) 
			{
				Console.WriteLine("Cannot Open File");
				Console.WriteLine(exc.Message);
				return; // File can't be opened, so stop the program.
			}

			// Read bytes until EOF is encountered.
			try 
			{
				do 
				{
					i = fin.ReadByte();
					if(i != -1) Console.Write((char) i);
				} while(i != -1);
			} 
			catch(IOException exc) 
			{
				Console.WriteLine("Error Reading File");
				Console.WriteLine(exc.Message);
			} 
			finally 
			{
				fin.Close();
			}
		}
	}
}	 

0 Comment's

Comment Form

Submit Comment