none
Difference between File and Stream

    Question

  • Hi all,

    What is the difference between file and stream?


    There i need to compare the following things.
                 physical place where they store data.
                 the way they store data
                
    The other thing is how a buffer differs from the file and stream?
    Where a buffer store data?
    What are the advantages of using streams and buffers?

    Thanks in advance.

    Jay.




     

    Sunday, May 17, 2009 7:34 AM

Answers

    • Stream is a representation of flow of data from one side to another e.g. from disk to memory and from memory to disk
    • Buffer is used to hold stream data temporarily.
    • File is a representation to store data on disk file. File uses streams to store and load data.
    • Marked as answer by Marco Zhou Monday, May 18, 2009 9:19 AM
    Monday, May 18, 2009 5:59 AM
  • The other major difference is that a stream does not necessarily need to stream from the hard drive.

    One of the advantages of streams versus directly file IO is that they isolate you from knowing what is on the other side of the stream.

    For example, if you use (old fashioned) C IO routines, you'd always be reading and writing from a FILE pointer directly.  Later, streams were added to isolate the code that reads from the file from the fact that it's a file.  .NET uses this method exclusively.

    There are many advantages -

    A "Stream" is just a source for data that you can put data into or take data out of.  There is no need to know what or how (or even if) that data will be used.  Often, you'd "stream" the data out of a file, but you can also "stream" the data from the network just as easily, with exactly the same code.

    In addition, since a stream doesn't say how the data will be accessed, only provides an interface for reading the data, you can also stream the data from another stream.  This makes it very easy to add other features like compression or encryption.  Instead of writing a file to disk, you write it to a stream that writes it to disk.  You can then very easily write it to a compression stream that writes to an encryption stream that writes to a file stream that writes to disk, without changing your code that does the writing.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by jaywith.7 Tuesday, May 19, 2009 1:51 AM
    Tuesday, May 19, 2009 1:12 AM

All replies

  •    file and stream are definitely  different.in .NET Framework Class Library they have different definations.

    1.
    File Class:Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.
    while
    Stream Class:Provides a generic view of a sequence of bytes.

    2.Store place :
    file usually are stored in CD ,DVD, harddisk  and SD card.
    stream usually are stored in computer 's memory
    3.File class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files. and Stream is the abstract base class of all streams. A stream is an abstraction of a sequence of bytes, such as a file, an input/output device, an inter-process communication pipe, or a TCP/IP socket.4.for more detail,you can visit the following pages:
    a.file:http://msdn.microsoft.com/en-us/library/system.io.file.aspx
    b.stream:http://msdn.microsoft.com/en-us/library/system.io.stream(VS.80).aspx


    you have a dream,you gonna protect it!
    Sunday, May 17, 2009 8:19 AM
  • hi Xu,

    Do you mean that stream is in RAM? So where the buffer resides?

    Thanks for the help!

     

    Jay.

     

    Sunday, May 17, 2009 10:12 AM
  • Here is an article which explains your query

    http://www.newlc.com/fr/topic-8648



    Thanks,
    A.m.a.L
    .Net Goodies
    Remember to click "mark as answered" when you get a correct reply to your question
    Sunday, May 17, 2009 10:28 AM
  • Yea,you are right.
     there are 3 subcassss which inherit fom abstract class Stream.
    1.FileStream Class
    2.MemoryStream Class
    3.BufferedStream Class
      when you Use the FileStream class to read from, write to, open, and close files on a file system,the obejct will load the file object data to the memory. FileStream buffers can hold the data templily which can improve performance.
       BufferedStream supplies a buffering layer to read and write operations on another stream. such as filestream.you also can set the buffersize when you new an object: int streamBufferSize = 1024,BufferedStream bStream = new BufferedStream(netStream, streamBufferSize))
      more details are here :
    1.http://msdn.microsoft.com/en-us/library/system.io.filestream(VS.80).aspx
    2.http://msdn.microsoft.com/en-us/library/system.io.bufferedstream(VS.80).aspx
    3.http://msdn.microsoft.com/en-us/library/system.io.memorystream(VS.80).aspx
    you have a dream,you gonna protect it!
    Sunday, May 17, 2009 11:45 AM
    • Stream is a representation of flow of data from one side to another e.g. from disk to memory and from memory to disk
    • Buffer is used to hold stream data temporarily.
    • File is a representation to store data on disk file. File uses streams to store and load data.
    • Marked as answer by Marco Zhou Monday, May 18, 2009 9:19 AM
    Monday, May 18, 2009 5:59 AM
  • Thanks Yazeem for the nice explanation.

    Then according to that explanation,

           File is in disk and Buffer is in memory (RAM) and Stream resides somewhere in between.

    Am I correct?

    If not, where the stream exists?

    Jay.

    Monday, May 18, 2009 11:58 PM
  •    file and stream are definitely  different.in .NET Framework Class Library they have different definations.

    1.
    File Class:Provides static methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.
    while
    Stream Class:Provides a generic view of a sequence of bytes.

    2.Store place :
    file usually are stored in CD ,DVD, harddisk  and SD card.
    stream usually are stored in computer 's memory
    3.File class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files. and Stream is the abstract base class of all streams. A stream is an abstraction of a sequence of bytes, such as a file, an input/output device, an inter-process communication pipe, or a TCP/IP socket.4.for more detail,you can visit the following pages:
    a.file:http://msdn.microsoft.com/en-us/library/system.io.file.aspx
    b.stream:http://msdn.microsoft.com/en-us/library/system.io.stream(VS.80).aspx


    you have a dream,you gonna protect it!

    If filestream get data to the memory why we need a buffer? Basically, what is the difference between buffer and memory stream?
    Tuesday, May 19, 2009 12:03 AM
  • The other major difference is that a stream does not necessarily need to stream from the hard drive.

    One of the advantages of streams versus directly file IO is that they isolate you from knowing what is on the other side of the stream.

    For example, if you use (old fashioned) C IO routines, you'd always be reading and writing from a FILE pointer directly.  Later, streams were added to isolate the code that reads from the file from the fact that it's a file.  .NET uses this method exclusively.

    There are many advantages -

    A "Stream" is just a source for data that you can put data into or take data out of.  There is no need to know what or how (or even if) that data will be used.  Often, you'd "stream" the data out of a file, but you can also "stream" the data from the network just as easily, with exactly the same code.

    In addition, since a stream doesn't say how the data will be accessed, only provides an interface for reading the data, you can also stream the data from another stream.  This makes it very easy to add other features like compression or encryption.  Instead of writing a file to disk, you write it to a stream that writes it to disk.  You can then very easily write it to a compression stream that writes to an encryption stream that writes to a file stream that writes to disk, without changing your code that does the writing.


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by jaywith.7 Tuesday, May 19, 2009 1:51 AM
    Tuesday, May 19, 2009 1:12 AM