none
StreamWriter, StreamReader RRS feed

  • Question

  • Hi All,

    I recently began studying C# in College and I'm wondering on the two methods of creating StreamWriter/StreamReader objects for File I/O.

    1. StreamWriter writer = new StreamWriter("FileName.txt", true); //true for append

    2. FileStream fileStreamWrite = new FileStream("FileName.txt", FileMode.OpenOrCreate, FileAccess.Write)
        StreamWriter writer = new StreamWriter(fileStreamWrite);

    What is the difference?

    Also, does StreamWriter and StreamReader use a FileStream object "underneath the hood" to write and read data to/from a file?

    Thanks!

    Joe

    Thursday, November 6, 2014 5:08 AM

Answers

  • If you wanna catch the difference between Streans and Readers/Writters check out this article in msdn docummentation.

    Generally streams are used as a bridge for reading and writting bytes between systems. From the other hand Readers\Writters are responsible for right encoding/conversions before sending data to stream.  

    Thursday, November 6, 2014 7:51 AM
  • The StreamWriter class can be initialized with a filename string or an already existing stream. If you pass a filename to  its constructor, it will create a stream for you under the hood. So in this particular case, the results will be pretty mych the same.

    It is very common for a class to have several different constructors to be used depending on the requirements of the calling code.

    Since the StreamWriter class has a constructor that accepts a string specifying the filename, there is no need to create a FileStream yourself unless you may be using this stream to something else than initializing the StreamWriter class.

    Please remember to mark helpful posts as answer to close the thread.

    Thursday, November 6, 2014 1:00 PM
  • 1. For a file and default encoding

    2. For a stream and selected encoding

    For case 1, if the file exists it will append data.

    For case 2, if the file exists it still overrides data.

    Thursday, November 6, 2014 2:44 PM

All replies

  • If you wanna catch the difference between Streans and Readers/Writters check out this article in msdn docummentation.

    Generally streams are used as a bridge for reading and writting bytes between systems. From the other hand Readers\Writters are responsible for right encoding/conversions before sending data to stream.  

    Thursday, November 6, 2014 7:51 AM
  • Hi,

    Refer this site:

    http://msdn.microsoft.com/en-in/library/aa903247(v=vs.71).aspx

    http://www.c-sharpcorner.com/UploadFile/prvn_131971/streamreader-and-streamwriter-classes-in-C-Sharp/

    This may help you to clear understand of StreamReader and StreamWriter..

    The StreamReader and StreamWriter classes extend the TextReader and TextWriter classes to provide the most widely used stream for writing textual data. In addition, these classes are encoding sensitive and can be used to read and write text in different encoded formats.

    Thanks,

    Thursday, November 6, 2014 11:42 AM
  • The StreamWriter class can be initialized with a filename string or an already existing stream. If you pass a filename to  its constructor, it will create a stream for you under the hood. So in this particular case, the results will be pretty mych the same.

    It is very common for a class to have several different constructors to be used depending on the requirements of the calling code.

    Since the StreamWriter class has a constructor that accepts a string specifying the filename, there is no need to create a FileStream yourself unless you may be using this stream to something else than initializing the StreamWriter class.

    Please remember to mark helpful posts as answer to close the thread.

    Thursday, November 6, 2014 1:00 PM
  • 1. For a file and default encoding

    2. For a stream and selected encoding

    For case 1, if the file exists it will append data.

    For case 2, if the file exists it still overrides data.

    Thursday, November 6, 2014 2:44 PM
  • Thanks all!  Really helpful :)
    Thursday, November 6, 2014 5:12 PM