FileStream vs/differences StreamWriter?

Question:

What is different between FileStream and StreamWriter in dotnet?

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

Is it possible to combine these two into one?

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 Text data like string or char to byte[] and then writes it 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.


FileStream writes bytes, StreamWriter writes text. That's all.


A FileStream is explicitly intended for working files.

A StreamWriter can be used to stream to any type of Stream - network sockets, files, etc.

ScottGu explains the different Stream objects quite nicely here: http://www.codeguru.com/Csharp/Csharp/cs_data/streaming/article.php/c4223


Well, from the MSDN for FileStream:

Exposes a Stream around a file, supporting both synchronous and asynchronous read and write operations.

and the MSDN for StreamWriter:

Implements a TextWriter for writing characters to a stream in a particular encoding.

The most obvious difference is that FileStream allows read/write operations, while StreamWriter is write only.

The StreamWriter page goes on to add:

StreamWriter is designed for character output in a particular encoding, whereas classes derived from Stream are designed for byte input and output.

So a second difference is that FileStream is for bytes, while StreamWriter is for text.


They are two different levels used in outputting information to known data sources.

A FileStream is a type of Stream, which is conceptually a mechanism that points to some location and can handle incoming and/or outgoing data to and from that location. Streams exist for reading/writing to files, network connections, memory, pipes, the console, debug and trace listeners, and a few other types of data sources. Specifically, a FileStream exists to perform reads and writes to the file system. Most streams are pretty low-level in their usage, and deal with data as bytes.

A StreamWriter is a wrapper for a Stream that simplifies using that stream to output plain text. It exposes methods that take strings instead of bytes, and performs the necessary conversions to and from byte arrays. There are other Writers; the other main one you'd use is the XmlTextWriter, which facilitates writing data in XML format. There are also Reader counterparts to the Writers that similarly wrap a Stream and facilitate getting the data back out.


One key difference (in addition to the above comments), could be that FileStream supports random disk acceess read and writes to any specified FileStream.Position. For large file modifications, that can be invaluable.


Need Your Help

Best method for reading newline delimited files and discarding the newlines?

python file readline

I am trying to determine the best way to handle getting rid of newlines when reading in newline delimited files in Python.