87

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?

Yousha Aleayoub
  • 3,221
  • 2
  • 42
  • 58
What'sUP
  • 12,102
  • 23
  • 71
  • 120

6 Answers6

101

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.

Henk Holterman
  • 236,989
  • 28
  • 287
  • 464
  • 4
    Also is important to note that `FileStream` is a type of stream, that is specifically tailored towards files. Streams natively work with bytes, however `StreamWriter` / Reader will write / read text on *any* stream, not just `FileStream`s. For example, `MemoryStreams`, `NetworkStreams`, etc.. – iliketocode Apr 12 '17 at 03:03
19

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

Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371
14

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

David
  • 68,722
  • 16
  • 125
  • 165
  • 1
    Excellent reference for streams, although it does not cover random access R/W streams. Especially the serialization/deserialization information and the demonstration of non-file streams are very useful. +1 – ThunderGr Oct 05 '13 at 08:26
7

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.

ChrisF
  • 127,439
  • 29
  • 243
  • 315
7

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.

Yousha Aleayoub
  • 3,221
  • 2
  • 42
  • 58
KeithS
  • 65,745
  • 16
  • 102
  • 161
1

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.

beanmf
  • 255
  • 2
  • 7