Advertisement



< Prev
Next >



C# Random Access Files



In this tutorial, we are going to explain how to perform the random access of files by using the Seek() method of FileStream class. The FileInputStream 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.
This method returns the new position of the file pointer in the stream.






The fields SeekOrigin Enum


Fields Description
SeekOrigin.Begin
Set the file pointer to the beginning of the file to seek from the beginning of the file.

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

SeekOrigin.End
>Set the file pointer to the end position in the file to seek from the end position in the file.






  • Calling the Seek() method of FileStream class.


  • In this example, we are writing a byte at a time to a new file by passing its full path to the constructor of FileStream. First, the file is created 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 first 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!.






    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