Advertisement



< Prev
Next >



C# Random Access Files



In this tutorial, we are going to explain how to perform the random access of a file by using the Seek() method of FileStream class. The FileStream class is used to create a byte stream, which is used to write or read bytes to/from a file. The FileStream class is defined in the System.IO namespace.




The Seek() method of FileStream class


Method Description
int Seek(int offset, SeekOrigin origin)
This method sets the current position of the file pointer, where:
  • offset, represents the point relative to origin from which to begin seeking.
  • origin, represents the beginning, the end, or the current position as a reference point for offset, using a value of type SeekOrigin enum(it is explained next)
This method returns the new position of the file pointer in the stream.






The fields of SeekOrigin Enum


Fields Description
SeekOrigin.Begin
Sets the file pointer to the beginning of the file, to seek from the beginning of a file.

SeekOrigin.Current
Sets the file pointer to the current position in the file, to seek from the current position.

SeekOrigin.End
Sets the file pointer to the end position in the file, to seek from the end position.






  • Calling the Seek() method to read a file from beginning to end.


  • In this example, first we will create a new file by passing its full path to the constructor of FileStream, and then we will perform the following operations:

    Note: We will use the FileMode field FileMode.Create which creates a new file and any existing file with the same name is overwritten.

    //C# calling the Seek() method of FileStream class to write to a file and to read from it.
    
    using System;
    using System.IO;
    using System.Text;
    
    
    class A
    {
    public static void Main(String[] ar)
    {
    	try
    	{
    		//Creating an object of FileStream
    		FileStream fs = new FileStream("D:\\Textbook8.txt", FileMode.Create);
    	
    		//Creating a String object 
    		String str = "There is a light at the end of the tunnel, so keep fighting!.";
    	
    		//Creating a byte array from a String
    		byte[] bA = Encoding.ASCII.GetBytes(str);  
    
    		Console.WriteLine("Writing to a file");
    		foreach(byte bt in bA)
    		{
    			//writing one byte at a time to the file.
    			fs.WriteByte(bt);	
    		}
    	
    		//Flushing the buffered date to the file
    		fs.Flush();
    
    		//Calling the Seek() method to set the file pointer to the beginning of the file
    		fs.Seek(0, SeekOrigin.Begin);
    
    		int c;
    	
    	
    		//Calling the ReadByte() method to reading one byte out of a file
    		Console.WriteLine("Reading the contents of the same file: ");
    		while((c = fs.ReadByte()) !=-1) 
    		{
    			//Casting byte to char for displaying the read character on the screen
    			Console.Write((char)c); 
    		}
    
    		//Closing the byte stream i.e. FileStream
    		fs.Close();	
    	}
    	catch(IOException e)
    	{
    		Console.WriteLine(e);
    	}
    
    }
    }
     


    Output is -:


    Writing to a file
    Reading the contents of the same file:
    There is a light at the end of the tunnel, so keep fighting!.







  • Calling the Seek() method to read a file, from somewhere in its middle to its end.




  • Note: We will use the FileMode field FileMode.Open which creates a open an existing file

    //C# calling the Seek() method of FileStream class 
    // to read a file from an offset somewhere in the middle to its end
    
    using System;
    using System.IO;
    using System.Text;
    
    
    class A
    {
    public static void Main(String[] ar)
    {
    	try
    	{
    		//Creating an object of FileStream
    		FileStream fs = new FileStream("D:\\Textbook8.txt", FileMode.Open);
    	
    		//Calling the Seek() method to set the file pointer 10 offset from the beginning of the file
    		fs.Seek(10, SeekOrigin.Begin);
    
    		int c;
    	
    	
    		//Calling the ReadByte() method to reading one byte out of a file
    		Console.WriteLine("Reading the contents of the same file: ");
    		while((c = fs.ReadByte()) !=-1) 
    		{
    			//Casting byte to char for displaying the read character on the screen
    			Console.Write((char)c); 
    		}
    
    		//Closing the byte stream i.e. FileStream
    		fs.Close();	
    	}
    	catch(IOException e)
    	{
    		Console.WriteLine(e);
    	}
    
    }
    }
     


    Output is -:


    Reading the contents of the same file:
     light at the end of the tunnel, so keep fighting!





    Please share this article -




    < Prev
    Next >
    < C# MemoryStream
    C# Serialization & Deserialization >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement