codescracker


c#

C# File I/O



« Previous Tutorial CodesCracker Home »


A file is basically a collection of data stored in the disk with specific name and directory path. And when a file is opened for reading/writing then it becomes stream.

C# Stream

Stream is simply the sequence of bytes passing through the communication path. There are two main types of streams in C#, which are:

C# File Handling Example Program

Here is an example program demonstrates file io (file handling) in C#:

/* C# File IO - C# File Handling - Example Program */
		
using System;
using System.IO;

namespace FileIOHandling
{
	class FileIOHandlingClass
	{
		static void Main(string[] args)
		{
			FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);
			for (int i=1; i<=20; i++)
			{
				F.WriteByte((byte)i);
			}
			
			F.Position = 0;
			for(int j=0; j<=20; j++)
			{
				Console.Write(F.ReadByte() + " ");
			}
			F.Close();
			Console.ReadKey();
		}	
	}
}

Now when we compile and run the above program then we will get the following output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1

« Previous Tutorial CodesCracker Home »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test