Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileStream vs/differences StreamWriter?

Question:

What is different between FileStream and StreamWriter in .Net?

What context are you supposed to use it? What is their advantage and disadvantage?

Is it possible to combine these two into one?

like image 280
What'sUP Avatar asked Feb 10 '11 22:02

What'sUP


People also ask

What is the difference between StreamWriter and TextWriter?

The StreamWriter class in C# is used for writing characters to a stream. It uses the TextWriter class as a base class and provides the overload methods for writing data into a file. The StreamWriter is mainly used for writing multiple characters of data into a file.

What is the difference between a StreamReader and StreamWriter objects?

A StreamReader is used whenever data is required to be read from a file. A Streamwriter is used whenever data needs to be written to a file.

What is the difference between MemoryStream and FileStream?

As the name suggests, a FileStream reads and writes to a file whereas a MemoryStream reads and writes to the memory. So it relates to where the stream is stored.

What is StreamReader and StreamWriter?

The StreamReader and StreamWriter classes are used for reading from and writing data to text files. These classes inherit from the abstract base class Stream, which supports reading and writing bytes into a file stream.


1 Answers

What is different between FileStream and StreamWriter in dotnet?

A FileStream is a Stream. Like all Streams it only deals with byte[] data.

A StreamWriter : TextWriter is a Stream-decorator. A TextWriter encodes the primitive type like string, int and char to byte[] and then writes hat to the linked Stream.

What context are you supposed to use it? What is their advantage and disadvantage?

You use a bare FileStream when you have byte[] data. You add a StreamWriter when you want to write text. Use a Formatter or a Serializer to write more complex data.

Is it possible to combine these two into one?

Yes. You always need a Stream to create a StreamWriter. The helper method System.IO.File.CreateText("path") will create them in combination and then you only have to Dispose() the outer writer.

like image 187
Henk Holterman Avatar answered Sep 23 '22 08:09

Henk Holterman