2

I have a confusion here in understanding the relationship between object fs and object sw in the following line of code.

StreamWriter sw = new StreamWriter(fs);

The confusion is in understanding whether,

1> Is sw just pointing to the object fs 2> when fs is passed as parameter to StreamWriter constructor which members are initialized to the contents of object fs.

Please explain the mechanism in detail how FileStream class and StreamWriter class are accomplishing the task through object references fs and sw.

     using System;

     using System.IO;

     class File_Write
        {
           public void Write_Data()
            {

                int empid = 12;
                string empname = "sean";



            FileStream fs = new FileStream("E:\\Files_Demo\\File_Write.txt", FileMode.Create,
                FileAccess.Write, FileShare.None);

            StreamWriter sw = new StreamWriter(fs)
            {
                sw.WriteLine("Good Morning");
                sw.WriteLine("Provide_EmployeeDetails");

                sw.WriteLine("Employee Id={0}", empid);


                Console.WriteLine("Written to file...Success");
                Console.Read();


            }
      }
Gt_R
  • 225
  • 1
  • 3
  • 10
  • related So question: http://stackoverflow.com/questions/5144794/what-does-stream-means-what-are-its-characteristics – Sam Axe Mar 06 '15 at 02:39
  • msdn explanation of files and streams: https://msdn.microsoft.com/en-us/library/k3352a4t(v=vs.110).aspx – Sam Axe Mar 06 '15 at 02:40

1 Answers1

3

A Stream is just a sequence of bytes that you can read, write, and/or seek (depending on the type of stream). It's just raw binary data, with no knowledge of what the data means and how to process it.

A StreamWriter is used to write text on a stream, using the specified text encoding (UTF-8 by default). Writing text directly on a Stream without using a StreamWriter is possible, but not very convenient (you would need to manually convert the string to bytes)

Similarly, a StreamReader is used to read text from a Stream.

1> Is sw just pointing to the object fs

It holds a reference to it, yes.

2> when fs is passed as parameter to StreamWriter constructor which members are initialized to the contents of object fs.

fs doesn't have a content; it's just a way to access the underlying data (in this case, a file on disk). So StreamWriter isn't "initialized to the content of object fs"; it just stores a reference to fs so that it can manipulate it later.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • I know that, however why fs is pased as parameter to streamwriter constructor. – Gt_R Mar 06 '15 at 02:40
  • I'm going to guess that if the OP doesn't have a grasp on files and streams they probably will also struggle with text encoding. – Sam Axe Mar 06 '15 at 02:41
  • 1
    @Gt_R, since a `StreamWriter` writes on a `Stream`, you need to give it a `Stream` when you create it. Passing `fs` to the `StreamWriter` constructor means that the writer will write on `fs`. – Thomas Levesque Mar 06 '15 at 02:43
  • @Gt_R: you pass the `FileStream` as a parameter to the `StreamWriter` ctor because if you didn't how would the `StreamWriter` know you wanted to operate on the `FileStream`? Remember, not all files are text files, so you may choose some other mechanism besides `StreamWriter` to operate on the `FileStream` depending on the data. – Sam Axe Mar 06 '15 at 02:44
  • @Gt_R, I completed my answer – Thomas Levesque Mar 06 '15 at 02:47
  • @ Thomas Levesque: Perfect!! :) – Gt_R Mar 06 '15 at 02:54
  • @Thomas Levesque: Good Point. very helpful. – Gt_R Mar 06 '15 at 02:55