Dot Net For All

Working With FileStream class(C#)

In my previous article I have discussed about the basics of the stream in .NET framework. As I have mentioned already in the article that the stream classes in C# always works on the top of backing stores which could be anything like FileSystem, Memory and Network .

 

FileStream’s backing store is File System. FIleStream class is present in the System.IO namespace and is derived from the Stream class.

We can work with FileStream on file system by following two ways.

  1. System.IO.File static methods.
  2. FileStream class.

Please note that all the examples here I am using are developed using C# language.

First we will look at the File class methods which can be used for reading and writing to the stream.

  1. OpenRead – First Methods which we can use for getting the read only stream from the file is OpenRead as Shown in the Below code snippet.
    FileStream fs = File.OpenRead(@"D:\Test.txt");

    This method is only useful only if we are sure that the file is present at the specified location. If not we will get an FileNotFoundException.

  2. OpenWrite – There are few other methods which are provided by the File class which can be helpful if we are not sure about the presence of the file. One of them is OpenWrite.This method opens an existing file or creates a new file for writing and returns a write only stream. It leaves existing content and sets position to 0 which means that if we start writing we can overwrite the existing content. Suppose if we want to append to existing content then we need to position the pointer to the end of the stream and start writing to it.
    FileStream fs = File.OpenWrite(@"D:\Test.txt");
  3. Create – This method creates a new file and returns a Read\Write Stream.It truncates the existing content and start writing the fresh content.
    FileStream fs = File.Create(@"D:\Test1.txt");

FileStream Class

We can retrieve the stream of a file using the constructors of FileStream Class.

The class provides around 15 overloaded constructors for reading and file and retrieving the stream as shown in the below figure.

The path in above case can be an absolute path, relative path or an UNC path for network locations.

FileMode Enumeration

As we can see in the above figure we can see that there is a parameter of type FileMode Enumeration which have following options.

  1. Create – Creates a new file. If the file already exist the file will be overwritten.
  2. CreateNew – Creates a new file but throws an “System.IO.IOException” exception if the file already exits.
  3. Open – Opens the file. Sets the pointer position to 0 and throws an “System.IO.FileNotFoundException” exception if the file is nto present.
  4. OpenOrCreate – Open an existing file or creates a new file if it already doesn’t exist.
  5. Truncate – Opens an exiting file and truncates its content to set its size to 0 bytes.
  6. Append – Opens a file and sets the position of the pointer to the end of the file.If file doesn’t exist it is created. Only Write Only mode is  allowed. Only appending of data is allowed.

FileAccess

Code Demo to Show FileStream class in Action

            using ( FileStream fs = new FileStream(@"D:\Test2.txt", FileMode.Create, FileAccess.ReadWrite))
            {
                fs.WriteByte(100);
                fs.Position = 0;
                fs.ReadByte();
            }

As we can see from the above code it is always necessary to implement the FileStream class in the using block otherwise it can create memory issues.

In this code I am creating a file in the R/W mode and writing and reading a byte to the same file.

In this article I have written about the FileStream Class which is used to read or write data to/from files.

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview