dcsimg
Results 1 to 3 of 3

Thread: [RESOLVED] Filestream vs Streamreader

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2018
    Posts
    42

    Resolved [RESOLVED] Filestream vs Streamreader

    Hi all,

    I have to read and write files and I'm not sure what is the best way. I've see the difference is Filestream read/write bytes and StreamReader/StreamWriter read/write text.

    I think I could use both of them, I've this great example of how to use stream:

    https://www.homeandlearn.co.uk/csharp/csharp_s11p1.html

    And here I've an explanation of File:

    https://docs.microsoft.com/es-es/dot...tframework-4.8

    I'll read not much long files and I'll completely read the content and copy on a string, or write the entire file from a string.

    Any suggest?

    Regards and thanks in advance

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,744

    Re: Filestream vs Streamreader

    The Stream class is a base class for types that allow binary data to be streamed from a source to a destination. The FileStream class inherits the Stream class and allows binary data to be streamed to and from the file system. Other classes that inherit Stream include MemoryStream and NetworkStream. The idea is that you can treat all those different types of streams in essentially the same way, regardless of the disparate sources and destinations for data.

    The TextReader and TextWriter classes are base classes that allow reading and writing of text. The StreamReader and StreamWriter classes inherit the TextReader and TextWriter and allow text to be read from and written to ANY Stream. They are most commonly used with a FileStream - they each have a constructor that will automatically create a FileStream from a file path - but they can be used with any type of Stream, e.g. if you want to write text over a network connection then you can create a StreamWriter over a NetworkStream.

    If you want to read or write binary data from or to a file then you will generally use a FileStream. If you want to read or write text from or to a file then you will generally use a StreamReader or StreamWriter, but they will each create and use a FileStream internally. If you are reading or writing binary data or text in one go, rather than in blocks, then you should generally use methods of the File class and let it worry about the rest. For example, to write out a text file in one go, call File.WriteAllText. It will create a StreamWriter internally, which will create a FileStream internally, so you don't have to worry about it. The File class also has ReadAllText, WriteAllBytes and WriteAllLines methods and more.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2018
    Posts
    42

    Re: Filestream vs Streamreader

    Many Thanks, jmcilhinney,

    I used File.FileWriteAllText, File.ReadAllText and File.AppendAlltext as you adviced me:

    https://docs.microsoft.com/en-us/dot...Text_Encoding_

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width