Share via


File IO System in .NET

I am preparing for MCTS Exam 70-536: Microsoft .NET Framework 2.0 - Application Development Foundation and came across the File IO System. So taking this opportunity here to post few details on the I/O streams to help understand the workings, specially for someone who is just getting started.

The following diagram below describes the I/O Class hierarchy in .Net Framework 2.0.

File IO System

Streams 

Represents the base class for many Stream based classes. Streams generally mean a sequence of bytes either flowing in to the system or flowing outside the system.

Specialised Streams These are streams specialized for a specific set of stream operations. Basically these streams inherit from the System.IO.Stream. The examples of such streams include :

  • System.IO.FileStream Represents a stream that can aid in reading and writing to a disk file or standard input or standard output.
  • System.IO.MemoryStream Represents a stream that can help in writing/reading to a memory location as a buffer, once the operation is done it can be transferred to another medium, perhaps to a disk file. MemoryStream's can reduce the need for temporary buffers or files in an application.
  • System.IO.BufferedStream Represents a block of bytes in memory acting as a cache and helps reduce the call to Operating System. Once the buffer operation is done the bytes can be transferred to the appropriate medium.

 

Writers And Readers

These classes help in reading/writing to specialized streams. As shown in the above class diagram we have specialized readers and writers. For text related read/write operations we can use StreamReader or StreamWriter and StringReader and StringWriter classes. All of these classes inherits from TextReader and TextWriter abstract base class.

The StreamWriters or StreamReaders helps in writing or reading text streams to or from the underlying stream. Whereas the StringWriter or StringReader helps in writing or reading from inline memory strings.

Another special kind of readers and writers are the BinaryReader and BinaryWriter classes.These classes help in reading/writing binary data.

Steps In Reading/Writing
  1. The first step is to have a stream representing the medium to which you want to read/write the data
  2. Have an appropriate reader/writer depending upon the type of data you are interested in
  3. Use the readers/writers method to perform the data read/write operation
A Demo

Though you can get more specific details on reading and writing to a file however I would like to put few lines to indicate my above theory on basic steps in Reading/Writing a file. So here we go

    1:    Try
    2:          Dim theFile As FileStream
    3:          theFile = File.Create(fileName)
    4:           Dim sw As New StreamWriter(theFile)
    5:           sw.WriteLine("this is a file")
    6:           sw.Close()
    7:    Catch ex As Exception
    8:          Console.WriteLine(ex.Message)
    9:    End Try

Comments

  • Anonymous
    July 24, 2008
    PingBack from http://blog.a-foton.ru/2008/07/file-io-system-in-net/
  • Anonymous
    July 24, 2008
    The diagram misses UnmanagedMemoryStream (though it's arguably a very specialized and rarely-used one, but still...).
  • Anonymous
    July 25, 2008
    This week I am coming to you from the Microsoft Campus. So as you would expect I have a lot of energy
  • Anonymous
    July 25, 2008
    Yes indeed, as you said we dont use UnmanagedMemoryStream unless their is need for you manage the memory yourself.
  • Anonymous
    July 27, 2008
    I'm also studying for the 70-536 exam.Got any more good resources, guides, free prep tests ? thanks in advance..